-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.z-字形变换.java
44 lines (37 loc) · 1 KB
/
6.z-字形变换.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
41
42
43
44
import java.util.List;
/*
* @lc app=leetcode.cn id=6 lang=java
*
* [6] Z 字形变换
*/
class Solution {
public String convert(String s, int numRows) {
if(numRows == 1){
return s;
}
int tempIndex = 0;
String tempStr = "";
int resultIndex = 0;
int j = 0;
List<String> list = new ArrayList();
for (int i = 0; i < numRows; i++) {
list.add("");
}
while (j < s.length()) {
tempIndex = j % (numRows * 2 - 2 );
if (tempIndex < numRows) {
resultIndex = tempIndex;
} else {
resultIndex = numRows * 2 - 2 - tempIndex;
}
tempStr = list.get(resultIndex).concat(String.valueOf(s.charAt(j)));
list.set(resultIndex, tempStr);
j++;
}
String result = "";
for (int i = 0; i < numRows; i++) {
result = result.concat(list.get(i));
}
return result;
}
}