Algorithm

Now we will try to resolve the problem as follow:

Firstly, we get the string s, such as "ABCDEFGHIJKLMNO...", and the three row ns, each equals 3, 4 and 5, as shown below:

- n = 3

```
A E I
B D F H J
C G K
```

- num Regular

```
0 4 8 ........+4
1 3 5 7 9 ........+2
2 6 10 ........+4
```

- n = 4

```
A G M
B F H L N
C E I K O
D J P
```

- num Regular

```
1 7 13 ........+6
2 6 8 12 14 ........+4/+2
3 5 9 11 15 ........+2/+4
4 10 16 ........+6
```

- n = 5

```
A I Q
B H J P R
C G K O S
D F L N T
E M U
```

- num Regular

```
1 9 17 ........+8
2 8 10 16 18 ........+6/+2
3 7 11 15 19 ........+4
4 6 12 14 20 ........+2/+6
5 13 21 ........+8
```

so

The first line always (n-1)*2

second ..........(n-2)*2/ {(n-1)*2 - (n-2)*2}

....................

last .......................(n-1)*2

so my java code :

```
public String convert(String s, int numRows) {
int point = numRows - 1, j = 0;
String s0 = "";
int i = point;
if(i == 0) return s;
int margin0 = i << 1;
int margin = 0;
while(i > 0) {
margin = i << 1;
j = point - i;
while(j < s.length()) {
s0 += s.charAt(j);
j += margin;
if(i != point) {
margin = margin0 - margin;
}
}
i--;
}
j = point - i;
margin = j << 1;
while(j < s.length()) {
s0 += s.charAt(j);
j += margin;
}
return s0;
}
```