Skip to content

Commit 124da31

Browse files
committed
Add solution and test-cases and README for problem 6
1 parent 31bc7a7 commit 124da31

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# [6.Zigzag Conversion][title]
2+
3+
## Description
4+
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)
5+
6+
```shell
7+
P A H N
8+
A P L S I I G
9+
Y I R
10+
```
11+
12+
And then read line by line: `"PAHNAPLSIIGYIR"`
13+
14+
Write the code that will take a string and make this conversion given a number of rows:
15+
16+
```shell
17+
string convert(string s, int numRows);
18+
```
19+
20+
**Example 1:**
21+
22+
```
23+
Input: s = "PAYPALISHIRING", numRows = 3
24+
Output: "PAHNAPLSIIGYIR"
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: s = "PAYPALISHIRING", numRows = 4
31+
Output: "PINALSIGYAHRPI"
32+
Explanation:
33+
P I N
34+
A L S I G
35+
Y A H R
36+
P I
37+
```
38+
39+
**Example 3:**
40+
41+
```
42+
Input: s = "A", numRows = 1
43+
Output: "A"
44+
45+
```
46+
## 结语
47+
48+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
49+
50+
[title]: https://leetcode.com/problems/zigzag-conversion/
51+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

leetcode/1-100/0006.ZigZag-Conversion/Solution.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package Solution
22

3-
import "fmt"
3+
import (
4+
"bytes"
5+
"fmt"
6+
)
47

58
func convert(s string, numRows int) string {
69
if numRows == 1 || numRows <= 0 || numRows > len(s) {
@@ -53,3 +56,26 @@ func convert2(s string, numRows int) string {
5356
}
5457
return string(result)
5558
}
59+
60+
func convert3(s string, numRows int) string {
61+
buf := bytes.NewBufferString("")
62+
if numRows == 1 {
63+
return s
64+
}
65+
66+
width := 2 * (numRows - 1)
67+
for idx := 0; idx < numRows; idx++ {
68+
step := 2 * idx
69+
if idx == 0 || idx == numRows-1 {
70+
step = width
71+
}
72+
for walker := idx; walker < len(s); walker += step {
73+
buf.WriteByte(s[walker])
74+
if step != width {
75+
step = width - step
76+
}
77+
}
78+
}
79+
80+
return buf.String()
81+
}

leetcode/1-100/0006.ZigZag-Conversion/Solution_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func TestSolution(t *testing.T) {
1515
}{
1616
{"1 test 1", "PAYPALISHIRING", 3, "PAHNAPLSIIGYIR"},
1717
{"2 test 2", "PAYPALISHIRING", 4, "PINALSIGYAHRPI"},
18+
{"3 test 3", "A", 1, "A"},
1819
}
1920

2021
// 开始测试
@@ -25,6 +26,16 @@ func TestSolution(t *testing.T) {
2526
t.Fatalf("expected: %v, but got: %v, with inputs: %v-%v",
2627
c.expect, ret, c.input1, c.input2)
2728
}
29+
ret = convert2(c.input1, c.input2)
30+
if !reflect.DeepEqual(ret, c.expect) {
31+
t.Fatalf("expected: %v, but got: %v, with inputs: %v-%v",
32+
c.expect, ret, c.input1, c.input2)
33+
}
34+
ret = convert3(c.input1, c.input2)
35+
if !reflect.DeepEqual(ret, c.expect) {
36+
t.Fatalf("expected: %v, but got: %v, with inputs: %v-%v",
37+
c.expect, ret, c.input1, c.input2)
38+
}
2839
})
2940
}
3041
}

0 commit comments

Comments
 (0)