|
18 | 18 | */ |
19 | 19 |
|
20 | 20 | public class _6 { |
21 | | - |
22 | | - public String convert(String s, int numRows) { |
23 | | - StringBuilder[] sb = new StringBuilder[numRows]; |
24 | | - char[] c = s.toCharArray(); |
25 | | - int len = s.length(); |
26 | | - for (int i = 0; i < numRows; i++) { |
27 | | - sb[i] = new StringBuilder();//this is an important step to initialize it |
28 | | - } |
29 | | - int i = 0; |
30 | | - while (i < len) { |
31 | | - for (int index = 0; index < numRows && i < len; index++) { |
32 | | - sb[index].append(c[i++]);// vertically down |
| 21 | + public static class Solution1 { |
| 22 | + public String convert(String s, int numRows) { |
| 23 | + StringBuilder[] sb = new StringBuilder[numRows]; |
| 24 | + char[] c = s.toCharArray(); |
| 25 | + int len = s.length(); |
| 26 | + for (int i = 0; i < numRows; i++) { |
| 27 | + sb[i] = new StringBuilder();//this is an important step to initialize it |
33 | 28 | } |
34 | | - |
35 | | - for (int index = numRows - 2; index >= 1 && i < len; index--) { |
36 | | - /**Why it should start from numRows - 2? Think of the example when numRows = 3 |
37 | | - the starting point of obliquely going up is 1, which is numRows-2.*/ |
38 | | - sb[index].append(c[i++]);// obliquely up |
| 29 | + int i = 0; |
| 30 | + while (i < len) { |
| 31 | + for (int index = 0; index < numRows && i < len; index++) { |
| 32 | + sb[index].append(c[i++]);// vertically down |
| 33 | + } |
| 34 | + |
| 35 | + for (int index = numRows - 2; index >= 1 && i < len; index--) { |
| 36 | + /**Why it should start from numRows - 2? Think of the example when numRows = 3 |
| 37 | + the starting point of obliquely going up is 1, which is numRows-2.*/ |
| 38 | + sb[index].append(c[i++]);// obliquely up |
| 39 | + } |
39 | 40 | } |
40 | | - } |
41 | 41 |
|
42 | | - for (i = 1; i < numRows; i++) { |
43 | | - sb[0].append(sb[i]); |
| 42 | + for (i = 1; i < numRows; i++) { |
| 43 | + sb[0].append(sb[i]); |
| 44 | + } |
| 45 | + return sb[0].toString(); |
44 | 46 | } |
45 | | - return sb[0].toString(); |
46 | 47 | } |
47 | 48 |
|
48 | 49 | } |
0 commit comments