-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.java
More file actions
39 lines (38 loc) · 1.06 KB
/
Copy pathConvert.java
File metadata and controls
39 lines (38 loc) · 1.06 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
package leetcode.string;
/*
* @description: 6.Z字形变换
* @param: 字符串和行数
* @return: Z变换后的字符串
* @author: cp
* @date: 2020/8/1
*/
public class Convert {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
StringBuilder[] builders = new StringBuilder[numRows];
for (int i = 0; i < numRows; i++) {
builders[i] = new StringBuilder();
}
int count = 0;
boolean addFlag = true;
for (int i = 0, length = s.length(); i < length; i++) {
builders[count].append(s.charAt(i));
if (addFlag) count++;
else count--;
if (count == numRows) {
addFlag = !addFlag;
count = count - 2;
}
if (count == -1) {
addFlag = !addFlag;
count = count + 2;
}
}
for (int i = 1; i < numRows; i++) {
builders[0].append(builders[i]);
}
return builders[0].toString();
}
}