题目描述 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)
1 2 3 P A H N A P L S I I G Y I R
And then read line by line: "PAHNAPLSIIGYIR"
解决方案 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include <bits/stdc++.h> using namespace std ;string zigzag (string s, int numRows) { if (numRows == 1 ) return s; string * r=new string [numRows]; for (int i = 0 ; i < numRows; ++i) { r[i] = "" ; } int groupSize = 2 * numRows - 2 ; int size = s.length(); for (int i = 0 ; i < numRows; ++i) { int j = i; while (j < size) { r[i] += s[j]; int k = 2 * (numRows - i - 1 ) ; if (k > 0 &&(i)&& ((j + k) < size)) r[i] += s[j + k]; j += groupSize; } } string res = "" ; for (int i = 0 ; i < numRows; ++i) { res += r[i]; } return res; } int main () { string s1 = "PAYPALISHIRING" ; int num1 = 3 ; cout << zigzag("AB" , 2 ) << "\n" ; system("pause" ); return 0 ; }
学习一个更精简的解决方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution {public : string convert (string s, int numRows) { if (numRows == 1 ) return s; vector <string > rows(min(numRows, int (s.size()))); int curRow = 0 ; bool goingDown = false ; for (char c : s) { rows[curRow] += c; if (curRow == 0 || curRow == numRows - 1 ) goingDown = !goingDown; curRow += goingDown ? 1 : -1 ; } string ret; for (string row : rows) ret += row; return ret; } };
复杂度比最上面的做法低,依次遍历总的字符串,比较巧的地方是改变子字符串的行号,用goingDown表示此时向上(+1)还是向下(-1)走,若是此时curRow是最上面一行或最下面一行,则变换方向,即goingDown取反。