6. Z字形变换
题目
描述:
将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。
以 "PAYPALISHIRING" 为例,排成 3 行时,每列中的字符如下。空白单元格表示该位置没有字符。
| 行 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| 第 1 行 | P | A | H | N | |||
| 第 2 行 | A | P | L | S | I | I | G |
| 第 3 行 | Y | I | R |
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"。
请你实现这个将字符串进行指定行数变换的函数: string convert(string s, int numRows);
示例 1:
- 输入:
s = "PAYPALISHIRING",numRows = 3 - 输出:
"PAHNAPLSIIGYIR"
示例 2:
- 输入:
s = "PAYPALISHIRING",numRows = 4 - 输出:
"PINALSIGYAHRPI" - 解释 按 4 行排列后,从上到下各行读出
PIN、ALSIG、YAHR、PI,拼接得到PINALSIGYAHRPI。
示例 3:
- 输入:
s = "A",numRows = 1 - 输出:
"A"
提示:
1 <= s.length <= 1000s由英文字母(小写和大写)、,和.组成1 <= numRows <= 1000
题解
只记录字符所属的行
题目要求先按 Z 字形放置字符,再按行读取。最终结果只关心每行有哪些字符以及它们的先后顺序,因此不必真的建立含有空白位置的二维矩阵。为每一行准备一个 StringBuilder,按输入顺序把字符追加到对应行,最后依次拼接各行即可。
用 currentRow 记录当前行号,goingDown 记录下一步是否向下。四行时,行号按 0,1,2,3,2,1,0,1,… 往返:到首行后向下,到末行后向上。代码先把字符放入当前行,再判断是否换向,最后更新行号。
初始行号为 0,goingDown 初始化为 false。放入第一个字符后,因为在首行,方向取反为 true,下一字符进入第 1 行。这样首行和末行可以共用同一个换向判断。
例如 s = "ABCDE"、numRows = 3,字符依次进入第 0,1,2,1,0 行,各行分别为 AE、BD、C,拼接得到 AEBDC。只有一行时没有往返过程;行数至少等于字符数时,每个字符依次落在单独一行,结果也与输入相同。这两种情况直接返回,避免继续模拟。
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
public String convert(String s, int numRows) {
if (numRows < 2 || numRows >= s.length()) {
return s;
}
List<StringBuilder> rows = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
rows.add(new StringBuilder());
}
int currentRow = 0;
boolean goingDown = false;
for (char c : s.toCharArray()) {
rows.get(currentRow).append(c);
if (currentRow == 0 || currentRow == numRows - 1) {
goingDown = !goingDown;
}
currentRow += goingDown ? 1 : -1;
}
StringBuilder res = new StringBuilder();
for (StringBuilder row : rows) {
res.append(row);
}
return res.toString();
}
设字符串长度为 n,非直接返回的情况下,分配行缓冲区、逐个写入字符、拼接各行,总时间为 O(n)。各行保存的字符总数为 n,加上字符数组和拼接缓冲区,辅助空间为 O(n)。
完整示例
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
50
51
52
53
54
55
56
import java.util.ArrayList;
import java.util.List;
public class Main {
public static String convert(String s, int numRows) {
if (numRows < 2 || numRows >= s.length()) {
return s;
}
List<StringBuilder> rows = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
rows.add(new StringBuilder());
}
int currentRow = 0;
boolean goingDown = false;
for (char c : s.toCharArray()) {
rows.get(currentRow).append(c);
if (currentRow == 0 || currentRow == numRows - 1) {
goingDown = !goingDown;
}
currentRow += goingDown ? 1 : -1;
}
StringBuilder res = new StringBuilder();
for (StringBuilder row : rows) {
res.append(row);
}
return res.toString();
}
public static void main(String[] args) {
String s1 = "PAYPALISHIRING";
int numRows1 = 3;
System.out.println("示例 1 输入: s = \"" + s1 + "\", numRows = " + numRows1);
System.out.println("示例 1 输出: \"" + convert(s1, numRows1) + "\"");
String s2 = "PAYPALISHIRING";
int numRows2 = 4;
System.out.println("示例 2 输入: s = \"" + s2 + "\", numRows = " + numRows2);
System.out.println("示例 2 输出: \"" + convert(s2, numRows2) + "\"");
String s3 = "A";
int numRows3 = 1;
System.out.println("示例 3 输入: s = \"" + s3 + "\", numRows = " + numRows3);
System.out.println("示例 3 输出: \"" + convert(s3, numRows3) + "\"");
}
}
运行结果(OpenJDK 21.0.12):
1
2
3
4
5
6
示例 1 输入: s = "PAYPALISHIRING", numRows = 3
示例 1 输出: "PAHNAPLSIIGYIR"
示例 2 输入: s = "PAYPALISHIRING", numRows = 4
示例 2 输出: "PINALSIGYAHRPI"
示例 3 输入: s = "A", numRows = 1
示例 3 输出: "A"
题目来源:LeetCode 官方题目。
