-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
38 lines (37 loc) · 826 Bytes
/
Copy pathSolution.java
File metadata and controls
38 lines (37 loc) · 826 Bytes
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
public class Solution
{
public String convert(String s, int numRows)
{
if (numRows <= 1)
{
return s;
}
StringBuilder[] sbArr = new StringBuilder[numRows];
for (int i = 0; i < numRows; i++)
{
sbArr[i] = new StringBuilder("");
}
int idx = 0;
int incr = 1;
int n = s.length();
for (int i = 0; i < n; i++)
{
sbArr[idx].append(s.charAt(i));
if (idx == 0)
{
incr = 1;
}
if (idx == numRows - 1)
{
incr = -1;
}
idx += incr;
}
String result = "";
for (StringBuilder sb : sbArr)
{
result += sb;
}
return result;
}
}