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 NA P L S I I GY 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
4
要搞成折线图,然后横着输出.建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> vec(nRows, vector ()); 6 7 int index=-1; 8 int tag=1; 9 for(int i=0;i