Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3cebea7

Browse files
committed
fix leetcode 0006 with go
1 parent 94bf633 commit 3cebea7

File tree

4 files changed

+117
-22
lines changed

4 files changed

+117
-22
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 6. Z 字形变换
2+
3+
## 链接
4+
https://leetcode-cn.com/problems/zigzag-conversion/
5+
6+
## 难度
7+
中等
8+
9+
## 描述
10+
比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:
11+
```text
12+
L C I R
13+
E T O E S I I G
14+
E D H N
15+
```
16+
17+
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。
18+
19+
请你实现这个将字符串进行指定行数变换的函数:
20+
21+
`string convert(string s, int numRows);`
22+
23+
示例 1:
24+
```text
25+
输入: s = "LEETCODEISHIRING", numRows = 3
26+
输出: "LCIRETOESIIGEDHN"
27+
```
28+
29+
示例 2:
30+
```text
31+
输入: s = "LEETCODEISHIRING", numRows = 4
32+
输出: "LDREOEIIECIHNTSG"
33+
解释:
34+
35+
L D R
36+
E O E I I
37+
E C I H N
38+
T S G
39+
```
40+
41+
## 思路
42+
一开始看题,会认为是一个二维的数组或者二维 slice
43+
其实就是一维的字符串数组 slice := make([]string, numRows)
44+
以示例 2 为例,压缩成一维字符串就是
45+
```text
46+
LDR
47+
EOEII
48+
ECIHN
49+
TSG
50+
```
51+
循环字符串,先往下每个字符串添加字符,然后往上每个字符串添加字符,依次循环。。
52+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package _006__ZigZag_Conversion
2+
3+
import (
4+
"strings"
5+
)
6+
7+
func convert(s string, numRows int) string {
8+
if numRows == 1 {
9+
return s
10+
}
11+
12+
slice := make([]string, numRows)
13+
14+
num, d := 0, 1; //d = 1 =》 向下,d = -1 =》 斜上
15+
for _, v := range s {
16+
slice[num] = slice[num] + string(v)
17+
num += d
18+
if num == numRows-1 {
19+
d = -1
20+
} else if num == 0 {
21+
d = 1
22+
}
23+
}
24+
return strings.Join(slice, "")
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package _006__ZigZag_Conversion
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestOne(t *testing.T) {
9+
tests := []struct {
10+
s string
11+
n int
12+
r string
13+
}{
14+
{
15+
s: "LEETCODEISHIRING",
16+
n: 3,
17+
r: "LCIRETOESIIGEDHN",
18+
},
19+
{
20+
s: "LEETCODEISHIRING",
21+
n: 4,
22+
r: "LDREOEIIECIHNTSG",
23+
},
24+
}
25+
26+
for _, v := range tests {
27+
assert.Equal(t, convert(v.s, v.n), v.r, "should be equal")
28+
}
29+
}

‎LeetCode/ProblemSet/006.ZigZag Conversion/zigzag-conversion.js‎

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,23 @@ var convert = function (s, numRows) {
88
return s;
99
}
1010

11-
let arr = Array(numRows).fill(0);
12-
for (let i = 0; i < numRows; i++) {
13-
arr[i] = [];
14-
}
11+
let array = new Array(numRows);
1512

16-
let x = 0, acol = true; // acol 是否是一列
13+
let num = 0, d = 1; //d = 1 =》 向下,d = -1 =》 斜上
1714
for (let i = 0, len = s.length; i < len; i++) {
18-
arr[x].push(s[i]);
19-
20-
if (acol && x !== numRows - 1) {
21-
x++;
22-
} else if (acol && x === numRows - 1) {
23-
x = numRows - 2;
24-
acol = false;
25-
} else if (!acol && x !== 0) {
26-
x--;
27-
} else if (!acol && x === 0) {
28-
acol = true;
29-
x++;
15+
array[num] = (array[num] ? array[num] : '') + s[i]
16+
num += d
17+
if (num === numRows - 1) {
18+
d = -1
19+
} else if (num === 0) {
20+
d = 1
3021
}
3122
}
32-
let r = '';
33-
arr.forEach(v => {
34-
r += v.join('');
35-
});
36-
return r;
23+
return array.join('')
3724
};
3825

26+
console.log(convert('LEETCODEISHIRING', 3));
27+
console.log(convert('LEETCODEISHIRING', 4));
3928
console.log(convert('PAYPALISHIRING', 3));
4029
// console.log(convert('PAYPALISHIRING', 4));
4130

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /