-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.java
More file actions
34 lines (31 loc) · 1.1 KB
/
Copy pathConvert.java
File metadata and controls
34 lines (31 loc) · 1.1 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
package self;
/**
* @author qiaoying
* @date 2019-04-25 14:24
*/
public class Convert {
public static String convert(String s, int numRows) {
if (s==null || s.length() <= 1 || numRows == 1)
return s;
int temp = 2*numRows - 2;
StringBuilder str = new StringBuilder();
for(int i = 0; i < numRows; i++){
//此时的k相当于上面的k*temp
for(int k = 0; k + i < s.length(); k += temp){
//本来弄了很多注释,后来全删了,感觉还是不要弄注释了吧
//假如不理解的话可以找几个字符串自己模拟想象下
str.append(s.charAt(k+i));
//如果是中间行则需要再加上一个字符
if(i != 0 && i != numRows-1 && k + temp - i < s.length()){
str.append(s.charAt(k+temp-i));
}
}
}
return str.toString();
}
public static void main(String[] args) {
String s = "LEETCODEISHIRING";
int numRows = 3;
System.out.println(convert(s,numRows));
}
}