n=2*(numRows-1);
第一行 +n (+0) +n (+0)…
第二行 +n-2 +2 +n-2 +2 …
第三行 +n-4 +4 +n-4 +4 …
….
第numRows行 (+0) +n (+0) +n …
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1) return s;
string ans;
int diff = 2 * (numRows - 1);
for (int k = 0; k < numRows; ++k) {
int l = k, d = k << 1;
d = d ? d : diff;
while (l < s.size()) {
ans += s[l];
if (d != diff) d = diff - d;
l += d;
}
}
return ans;
}
};