class Solution {
public:
string convert(string s, int nRows) {
int n = s.length();
string result;
if(nRows == 1) {
return s;
}
int windowSize = (nRows - 1) * 2;
for(int i = 0; i < nRows; i++) {
int p = 0;
int q = 0;
for(int j = 0; ;j++) {
p = i + windowSize * j;
if(p >= n) break;
result += s[p];
q = windowSize * (j + 1 ) - i;
if(q >= n) break;
if(p != q && q < windowSize * (j + 1)) {
result += s[q];
}
}
}
return result;
}
};
3/30/2014
Leetcode -- ZigZag Conversion
Solution: 03/30/2014
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment