Skip to content

Commit ef36b62

Browse files
refactor 6
1 parent 45a57d0 commit ef36b62

File tree

1 file changed

+22
-21
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+22
-21
lines changed

src/main/java/com/fishercoder/solutions/_6.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,32 @@
1818
*/
1919

2020
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
3328
}
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+
}
3940
}
40-
}
4141

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();
4446
}
45-
return sb[0].toString();
4647
}
4748

4849
}

0 commit comments

Comments
 (0)