-
Notifications
You must be signed in to change notification settings - Fork 4
/
ZigzagConversion.java
36 lines (33 loc) · 1.05 KB
/
ZigzagConversion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.dbc;
import java.util.ArrayList;
import java.util.List;
public class ZigzagConversion {
public static String convert(String s, int numRows) {
List<Character>[] resMatrix = new List[numRows];
for (int i = 0; i < numRows; i++) {
resMatrix[i] = new ArrayList<>();
}
int curPoint = 0, flag = 0;
for (int i = 0; i < s.length(); i++) {
resMatrix[curPoint].add(s.charAt(i));
if (flag == 0 && curPoint + 1 < numRows) {
curPoint++;
} else if (flag == 0 && curPoint - 1 >= 0) {
curPoint--;
flag = 1;
} else if (curPoint - 1 >= 0) {
curPoint--;
} else if (curPoint + 1 < numRows) {
curPoint++;
flag = 0;
}
}
StringBuffer res = new StringBuffer();
for (List<Character> chList : resMatrix) {
for (Character ch : chList) {
res.append(ch);
}
}
return res.toString();
}
}