-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathSolution.java
More file actions
45 lines (42 loc) · 1.19 KB
/
Copy pathSolution.java
File metadata and controls
45 lines (42 loc) · 1.19 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
package 字符串操作.q6_Z字形变换;
import java.util.ArrayList;
import java.util.List;
/**
* o(n) 可用一boolean变量代替求余操作
*/
public class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
int len = s.length();
int col = 0;
int n = 0;
List<StringBuffer> list = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
StringBuffer temp = new StringBuffer();
list.add(temp);
}
while (n < len) {
int y = col % (numRows - 1);
if (y == 0) {
for (int i = 0; i < numRows && n < len; i++) {
list.get(i).append(s.charAt(n));
n++;
}
} else {
list.get(numRows - 1 - y).append(s.charAt(n));
n++;
}
col++;
}
String rs = "";
for (int i = 0; i < list.size(); i++) {
rs += list.get(i).toString();
}
return rs;
}
public static void main(String[] args) {
System.out.println(new Solution().convert("LEETCODEISHIRING", 4));
}
}