Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
40 lines (32 loc) · 1.11 KB

File metadata and controls

40 lines (32 loc) · 1.11 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package Algorithms.string;
public class Convert {
public static void main(String[] strs) {
System.out.println(convert("A", 1));
}
public static String convert(String s, int nRows) {
if (s == null) {
return null;
}
// 第一个小部分的大小
int size = 2 * nRows - 2;
// 当行数为1的时候,不需要折叠。
if (nRows <= 1) {
return s;
}
StringBuilder ret = new StringBuilder();
int len = s.length();
for (int i = 0; i < nRows; i++) {
// j代表第几个BLOCK
for (int j = i; j < len; j += size) {
ret.append(s.charAt(j));
// 即不是第一行,也不是最后一行,还需要加上中间的节点
int mid = j + size - i * 2;
if (i != 0 && i != nRows - 1 && mid < len) {
char c = s.charAt(mid);
ret.append(c);
}
}
}
return ret.toString();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.