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".

將字元進行Z字型轉換,然後進行儲存,一開始沒有想到什麼好方法,看了下別人的實現,可以做的比較簡單,每一行用一個字串記錄下來之後,然後拼接起來就可以,非滿列字串是有規律的,具體見下程式碼:

 class Solution {
public:
string convert(string s, int numRows) {
vector<string> res;
res.resize(numRows);
string ret;
int gap = numRows - ;
int i = , j;
while(i < s.size()){
for(j = ; i < s.size() && j < numRows; ++i, ++j)
res[j] += s[i];
for(j = gap; i < s.size() && j > ; ++i, --j)
res[j] += s[i];
}
for(int k = ; k < numRows; ++k){
ret += res[k];
}
return ret;
}
};