Skip to content

Commit 625db12

Browse files
committed
update the 38 problem solution
1 parent 3af63bb commit 625db12

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ LeetCode of algorithms with golang solution(updating:smiley:).
4242

4343
## Community
4444

45-
* [ACWING](https://www.acwing.com/) 一些算法竞赛大佬创建的平台,挺适合入门的。
46-
* [leetbook](https://github.com/hk029/leetbook) 某位大佬写的Leetcode题解,不过已经不更新了
47-
* [LeetCode-in-Go](https://github.com/aQuaYi/LeetCode-in-Go) 某位算法大佬的Golang题解
45+
- [ACWING](https://www.acwing.com/) 一些算法竞赛大佬创建的平台,挺适合入门的。
46+
- [leetbook](https://github.com/hk029/leetbook) 某位大佬写的Leetcode题解,不过已经不更新了
47+
- [WANG leetcode](https://github.com/wind-liang/leetcode) 某位朋友的Leetcode题解。经常更新,解释到位。
48+
- [LeetCode-in-Go](https://github.com/aQuaYi/LeetCode-in-Go) 某位算法大佬的Golang题解
4849

4950
## Contributors
5051

src/0038.Count-and-Say/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,21 @@ Output: "1211"
4040

4141

4242
## 题解
43+
> 题意:这道题的意思特别刚,不太好理解
44+
45+
```
46+
初始值第一行是 1。
47+
第二行读第一行,1 个 1,去掉个字,所以第二行就是 11。
48+
第三行读第二行,2 个 1,去掉个字,所以第三行就是 21。
49+
第四行读第三行,1 个 2,1 个 1,去掉所有个字,所以第四行就是 1211。
50+
第五行读第四行,1 个 1,1 个 2,2 个 1,去掉所有个字,所以第五航就是 111221。
51+
第六行读第五行,3 个 1,2 个 2,1 个 1,去掉所以个字,所以第六行就是 312211。
52+
然后题目要求输入 1 - 30 的任意行数,输出该行是啥。
53+
```
54+
4355
### 思路1
44-
在遍历数组时用 Stack 把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。
56+
直接按照从2到n的顺序生成字符串,即每次找连续相同的数字段,合并。
57+
4558
```go
4659
func countAndSay(n int) string {
4760
if n < 1 {

src/0038.Count-and-Say/Solution.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package Solution
22

3+
import "fmt"
4+
35
func countAndSay(n int) string {
46
if n < 1 {
57
return ""
@@ -13,6 +15,7 @@ func countAndSay(n int) string {
1315
cnt++
1416
if j == len(say)-1 || say[j] != say[j+1] {
1517
nextSay = append(nextSay, 48+cnt, say[j]) // 48 - the ASCII code point of "0"
18+
fmt.Println(string(nextSay), string(48+cnt), string( say[j]))
1619
cnt = 0
1720
}
1821
}
Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
package Solution
22

33
import (
4+
"reflect"
5+
"strconv"
46
"testing"
57
)
68

79
func TestSolution(t *testing.T) {
8-
t.Run("Test-1", func(t *testing.T) {
9-
got := countAndSay(1)
10-
want := "1"
11-
if got != want {
12-
t.Error("GOT:", got, "WANT:", want)
13-
}
14-
})
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs int
14+
expect string
15+
}{
16+
{"TestCase", 1, "1"},
17+
{"TestCase", 4, "1211"},
18+
}
1519

16-
t.Run("Test-2", func(t *testing.T) {
17-
got := countAndSay(4)
18-
want := "1211"
19-
if got != want {
20-
t.Error("GOT:", got, "WANT:", want)
21-
}
22-
})
20+
// 开始测试
21+
for i, c := range cases {
22+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
23+
got := countAndSay(c.inputs)
24+
if !reflect.DeepEqual(got, c.expect) {
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26+
c.expect, got, c.inputs)
27+
}
28+
})
29+
}
30+
}
31+
32+
// 压力测试
33+
func BenchmarkSolution(b *testing.B) {
34+
35+
}
36+
37+
// 使用案列
38+
func ExampleSolution() {
2339

2440
}

0 commit comments

Comments
 (0)