zoukankan      html  css  js  c++  java
  • 【LeetCode】6

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

    P   A   H   N
    A P L S I I G
    Y   I   R
    

    And then read line by line: "PAHNAPLSIIGYIR" 

    Write the code that will take a string and make this conversion given a number of rows:

    string convert(string text, int nRows);

    convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

    Solution:原来是说:

    1      7
    2   6 8
    3 5   9
    要搞成折线图,然后横着输出.建nRows个数组,将s一个一个装进下标为0,1....nRows-1....1,0的数组里,两层vector
     1 class Solution {
     2 public:
     3     string convert(string s, int nRows) {
     4        if(nRows==1)return s;
     5        vector<vector<char>> vec(nRows, vector<char>());
     6        
     7        int index=-1;
     8        int tag=1;
     9        for(int i=0;i<s.size();i++){
    10            index += tag;
    11            vec[index].push_back(s[i]);
    12            if(index==nRows-1)tag=-1;
    13            else{
    14                if(index==0)tag=1;
    15            }
    16        }
    17        string ret;
    18        for(int i=0;i<nRows;i++){
    19            for(int j=0;j<vec[i].size();j++){
    20                ret+=vec[i][j];
    21            }
    22        }
    23        return ret;
    24     }
    25 };
     
  • 相关阅读:
    raspi扩展板
    树莓派学习笔记——I2C设备载入和速率设置
    python多线程(四)
    python多线程(三)
    python多线程(二)
    python3.x对python2.x变动
    python多线程(一)
    raspi集成库及安装
    eclipse软件安装及python工程建立
    原型模式
  • 原文地址:https://www.cnblogs.com/irun/p/4696748.html
Copyright © 2011-2022 走看看