-
Notifications
You must be signed in to change notification settings - Fork 1
/
Zconvert.java
40 lines (37 loc) · 1.11 KB
/
Zconvert.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
37
38
39
40
import java.util.ArrayList;
/**
* ZigZag Conversion.
* @author LBW
*/
public class Zconvert {
public String convert(String s, int numRows) {
if (numRows == 1)
return s;
//init lists
ArrayList<ArrayList<Character>> lists = new ArrayList<>(numRows);
for (int i = 0; i < numRows; i++) {
ArrayList<Character> list = new ArrayList<>();
lists.add(list);
}
int len = s.length();
//put char into arrayList
for (int i = 0; i < len; i++) {
int j = i % (2*numRows-2);
j = j < numRows ? j : 2*numRows-2-j;
lists.get(j).add(s.charAt(i));
}
//get result
StringBuilder result = new StringBuilder();
for (ArrayList<Character> list: lists) {
for (Character c: list) {
result.append(c);
}
}
return result.toString();
}
public static void main(String[] args) {
Zconvert zconvert = new Zconvert();
String result = zconvert.convert("PAYPALISHIRING", 3);
System.out.println(result);
}
}