Leetcode#6 ZigZag Conversion
Published:
Link:
https://leetcode.com/problems/zigzag-conversion/description/
Idea:
Convert index reversely.
Solution:
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1)
return s;
vector<vector<char>> res(numRows, vector<char>(0, 0));
int factor = numRows + numRows - 2;
for(int i=0; i<s.size(); i++) {
int index = i%factor;
int row_idx = index < numRows ? index : (factor-index);
res[row_idx].push_back(s[i]);
}
string tmp = "";
for (int i = 0; i < numRows; i++){
for(int j=0; j < res[i].size();j++){
tmp = tmp + res[i][j];
}
}
return tmp;
}
};