-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZConversion.java
More file actions
46 lines (37 loc) · 1.06 KB
/
Copy pathZConversion.java
File metadata and controls
46 lines (37 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
40
41
42
43
44
45
46
package LeetCode;
/***
* Leetcode No 6 Medium Level
*/
public class ZConversion {
/***
*
* 这是我自己的纯找规律解法。。
* 一点也不美丽。。
*
*/
public String solution(String s, int numRows) {
if (s.length() <= numRows || numRows == 1) return s;
StringBuilder ret = new StringBuilder();
int index = 0;
while(index < s.length()) {
ret.append(s.charAt(index));
index += numRows + numRows - 2;
}
for(int i=1; i<numRows-1; i++) {
index = i;
while(index < s.length()) {
ret.append(s.charAt(index));
if (index+numRows*2-2-i-i < s.length()) {
ret.append(s.charAt(index+numRows*2-2-i-i));
}
index += numRows + numRows - 2;
}
}
index = numRows-1;
while(index < s.length()) {
ret.append(s.charAt(index));
index += numRows + numRows - 2;
}
return ret.toString();
}
}