LeetCode题目：ZigZag Conversion

By | 2012 年 11 月 8 日

``` nRows = 2
0 2 4 6 ...
1 3 5 7
```
``` nRows = 3
0   4   8  ...
1 3 5 7 9
2   6   10
```
``` nRows = 4
0     6       12 ...
1   5 7    11
2 4   8 10
3     9
```

ZigZag Conversion
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”.

```class Solution {
public:
string convert(string s, int nRows) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(nRows <= 1) return s;
string ret;
int zigsize = 2 * nRows - 2;
for(int i = 0; i < nRows; ++i) {
for(int base = i; ;base += zigsize) {
if(base >= s.size())
break;
ret.append(1,s[base]);
if(i > 0 && i < nRows - 1) {