The distribution of the elements is period.

```
P A H N
A P L S I I G
Y I R
```

For example, the following has 4 periods(cycles):

```
P | A | H | N
A P | L S | I I | G
Y | I | R |
```

The size of every period is defined as "cycle"

```
cycle = (2*nRows - 2), except nRows == 1.
```

In this example, (2*nRows - 2) = 4.

In every period, every row has 2 elements, except the first row and the last row.

Suppose the current row is i, the index of the first element is j:

```
j = i + cycle*k, k = 0, 1, 2, ...
```

The index of the second element is secondJ:

```
secondJ = (j - i) + cycle - i
```

(j-i) is the start of current period, (j-i) + cycle is the start of next period.

```
string convert(string s, int nRows) {
if(nRows <= 1) return s;
string result = "";
//the size of a cycle(period)
int cycle = 2 * nRows - 2;
for(int i = 0; i < nRows; ++i)
{
for(int j = i; j < s.length(); j = j + cycle){
result = result + s[j];
int secondJ = (j - i) + cycle - i;
if(i != 0 && i != nRows-1 && secondJ < s.length())
result = result + s[secondJ];
}
}
return result;
}
```