Skip to content

Commit b122d2a

Browse files
authored
Merge pull request #60 from bluesword12350/master
6. ZigZag Conversion (java)
2 parents 242d09a + 270095d commit b122d2a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public String convert(String s, int numRows) {
3+
if (numRows == 1) return s;
4+
StringBuilder result = new StringBuilder();
5+
int group = 2 * numRows - 2;
6+
for (int i = 1; i <= numRows; i++) {
7+
int interval = 2 * numRows - 2 * i;
8+
if (i == numRows) interval = 2 * numRows - 2;
9+
int index = i;
10+
while (index <= s.length()) {
11+
result.append(s.charAt(index - 1));
12+
index += interval;
13+
interval = group - interval;
14+
if (interval == 0) interval = group;
15+
}
16+
}
17+
return result.toString();
18+
}
19+
}

0 commit comments

Comments
 (0)