MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / convert

Method convert

src/com/blankj/medium/_0006/Solution.java:35–56  ·  view source on GitHub ↗
(String s, int numRows)

Source from the content-addressed store, hash-verified

33// }
34
35 public String convert(String s, int numRows) {
36 if (numRows <= 1) return s;
37 int len = s.length();
38 char[] chars = s.toCharArray();
39 StringBuilder[] sbs = new StringBuilder[numRows];
40 for (int i = 0; i < numRows; i++) {
41 sbs[i] = new StringBuilder();
42 }
43 int i = 0;
44 while (i < len) {
45 for (int j = 0; j < numRows && i < len; ++j) {
46 sbs[j].append(chars[i++]);
47 }
48 for (int j = numRows - 2; j >= 1 && i < len; --j) {
49 sbs[j].append(chars[i++]);
50 }
51 }
52 for (i = 1; i < numRows; i++) {
53 sbs[0].append(sbs[i]);
54 }
55 return sbs[0].toString();
56 }
57
58 public static void main(String[] args) {
59 Solution solution = new Solution();

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected