-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
executable file
·57 lines (54 loc) · 1.65 KB
/
Solution.java
File metadata and controls
executable file
·57 lines (54 loc) · 1.65 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package $006;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 16:35 2018/3/26.
*/
public class Solution {
public String convert(String s, int numRows) {
if (s == null || s.length() == 0 || numRows == 1){
return s;
}
StringBuffer[] lists = new StringBuffer[numRows];
for (int i = 0; i < numRows; i++){
lists[i] = new StringBuffer();
}
boolean flag = true;
int j = 1;
for (; (j + numRows - 1) <= s.length();){
if (flag){
for (int i = 0; i < numRows - 1; i++){
lists[i].append(s.charAt(j-1+i));
}
flag = false;
}else {
for (int i = 0; i < numRows - 1; i++){
lists[numRows-1-i].append(s.charAt(j-1+i));
}
flag = true;
}
j+= (numRows-1);
}
// j -= (numRows-1);
if (flag){
for (int i = 0; j+i <= s.length(); i++){
lists[i].append(s.charAt(j-1+i));
}
}else {
for (int i = 0; j+i <= s.length(); i++){
lists[numRows-1-i].append(s.charAt(j-1+i));
}
}
StringBuffer re = new StringBuffer();
for (int i = 0; i < numRows; i++){
re.append(lists[i].toString());
}
return re.toString();
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.convert("PAYPALISHIRING", 3));
}
}