LeetCode - ZigZag Conversion
Problem description
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
1
2
3
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
1
string convert(string s, int numRows);
Example 1:
1
2
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
1
2
3
4
5
6
7
8
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Analysis
Basic idea is to construct the grid, first divide the grid into several groups. Like this:
1
2
3
4
P | I | N
A L | S I | G
Y A | H R |
P | I |
And the group contains numRows * 2 - 2 character in string, and the width of the grid should be numGroups * (numRows - 1), and we can construct the grid and use a StringBuilder to get the output.
Here’s the code:
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
39
40
41
42
43
44
45
46
47
48
49
public class ZigZagConversion {
public String convert(String s, int numRows) {
int len = s.length();
// miss this part
if (numRows == 1){
return s;
}
int group = numRows * 2 - 2;
int numGroups = len / group + 1;
int width = numGroups * (numRows - 1);
char[][] grid = new char[numRows][width];
for (int i = 0; i < numRows; i++) {
Arrays.fill(grid[i], ' ');
}
int index = 0;
for (int i = 0; i < numGroups; i++) {
int innerIndex = 0;
while (index < s.length() && innerIndex < group) {
char c = s.charAt(index);
int col = i * (numRows - 1);
if (innerIndex < numRows) {
grid[innerIndex][col] = c;
} else {
int j = innerIndex - numRows + 1;
grid[numRows - 1 - j][col + j] = c;
}
index++;
innerIndex++;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < width; j++) {
if (grid[i][j] != ' ') {
sb.append(grid[i][j]);
}
}
}
return sb.toString();
}
}
A better solution without extra space but same logic is:
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
class Solution {
public static String convert(String s, int numRows) {
if(numRows == 1)
return s;
StringBuilder res = new StringBuilder();
for (int i = 0; i < numRows; i++) {
int first = (numRows - i) * 2 - 2;
int second = i * 2;
int flag = 0;
if (i == 0) {
for (int j = i; j < s.length(); j += first)
res.append(s.charAt(j));
} else if (i == numRows - 1) {
for (int j = i; j < s.length(); j += second)
res.append(s.charAt(j));
} else {
for (int j = i; j < s.length(); ) {
res.append(s.charAt(j));
if (flag == 0) {
j += first;
flag = 1;
} else {
j += second;
flag = 0;
}
}
}
}
return res.toString();
}
}
Another simple solution is to use several StringBuilder to indicate different rows and add character from top to bottom and bottom to top.
Here’s the main code:
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
String test(String s, int numRows){
if (numRows == 1){
return s;
}
StringBuilder[] sbs = new StringBuilder[numRows];
for(int i = 0; i < numRows; i++){
sbs[i] = new StringBuilder();
}
boolean flag = true; // true for top to bottom, else for bottom to top
int index = 0;
for(char c:s.toCharArray()){
if (flag){
sbs[index++].append(c);
if (index == numRows){
index-=2;
flag = false;
}
}
else {
sbs[index--].append(c);
if (index < 0){
index+=2;
flag = true;
}
}
}
String res = "";
for(StringBuilder sb:sbs){
res+=sb.toString();
}
return res;
}
What to improve
- be careful about divide by zero exception