-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_6.java
More file actions
34 lines (30 loc) · 852 Bytes
/
sort_6.java
File metadata and controls
34 lines (30 loc) · 852 Bytes
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
import java.util.ArrayList;
public class sort_6 {
public static String convert(String s, int nRows) {
if (s == null || s.isEmpty()) {
return s;
}
int length = s.length();
if (length <= nRows || nRows == 1) {
return s;
}
StringBuilder sb = new StringBuilder();
int step = 2 * (nRows - 1);
for (int i = 0; i < nRows; i++){
int interval=step-2*i;
for(int j=i;j<length;j+=step){
sb.append(s.charAt(j));
//每一轮以插值的形式插入
if(i!=0&&i!=nRows-1&&j+interval<length){
sb.append(s.charAt(j+interval));
}
}
}
return sb.toString();
}
public static void main(String[] args) {
String string="PAYPALISHIRING";
int rows=3;
System.out.println(convert(string, rows));
}
}