-
Notifications
You must be signed in to change notification settings - Fork 1
/
Question6.java
executable file
·46 lines (43 loc) · 1.24 KB
/
Question6.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
45
46
package me.stormma.leetcode.string;
/**
* 难度系数: 3星
* leetcode 6
* @author stormma
* @date 2017/11/05
*/
public class Question6 {
static class Solution {
public String convert(String s, int numRows) {
if (numRows <= 1 || s.length() <= numRows) {
return s;
}
StringBuilder[] ans = new StringBuilder[numRows];
for (int i = 0; i < numRows; i++) {
ans[i] = new StringBuilder("");
}
int index = 0;
boolean inc = true;
for (int i = 0; i < s.length(); i++) {
ans[index].append(s.charAt(i));
if (index == 0) {
inc = true;
}
if (index == numRows - 1) {
inc = false;
}
if (inc) {
index++;
} else {
index--;
}
}
for (int i = 1; i < ans.length; i++) {
ans[0].append(ans[i].toString());
}
return ans[0].toString();
}
}
public static void main(String[] args) {
System.out.println(new Solution().convert("PAYPALISHIRING", 3));
}
}