Skip to content

Commit cfc1bdb

Browse files
aQuaaQua
aQua
authored and
aQua
committed
adding Problem 20
1 parent f4aeb60 commit cfc1bdb

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# [20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)
22

33
## 题目
4+
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
45

6+
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
57

68
## 解题思路
7-
9+
[](https://zh.wikipedia.org/wiki/%E5%A0%86%E6%A0%88)是一个后进先出的队列,用在这里可以避免复杂的判断结构。但是,Go语言的标准库没有栈这种结构,我就手动实现了一个。
810

911
## 总结
10-
11-
12+
选用合适的数据结构,可以让程序清晰。
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
11
package Problem0020
22

3+
func isValid(str string) bool {
4+
s := new(stack)
5+
6+
for _, b := range str {
7+
switch b {
8+
case '(', '[', '{':
9+
s.push(b)
10+
case ')', ']', '}':
11+
if r, ok := s.pop(); !ok || r != matching[b] {
12+
// !ok 说明“([{”的数量,小于")]}"的数量
13+
return false
14+
}
15+
}
16+
}
17+
18+
// len(*s) > 0 说明"([{"的数量,大于")]}"的数量
19+
if len(*s) > 0 {
20+
return false
21+
}
22+
23+
return true
24+
}
25+
26+
var matching = map[rune]rune{
27+
')': '(',
28+
']': '[',
29+
'}': '{',
30+
}
31+
32+
type stack []rune
33+
34+
func (s *stack) push(b rune) {
35+
*s = append(*s, b)
36+
}
37+
38+
func (s *stack) pop() (rune, bool) {
39+
if len(*s) > 0 {
40+
res := (*s)[len(*s)-1]
41+
*s = (*s)[:len(*s)-1]
42+
return res, true
43+
}
44+
return 0, false
45+
}

Algorithms/0020.valid-parentheses/valid-parentheses_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0020
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -21,7 +21,7 @@ type para struct {
2121
// ans 是答案
2222
// one 代表第一个答案
2323
type ans struct {
24-
one string
24+
one bool
2525
}
2626

2727
func Test_Problem0020(t *testing.T) {
@@ -30,17 +30,41 @@ func Test_Problem0020(t *testing.T) {
3030
qs := []question{
3131

3232
question{
33-
para{""},
34-
ans{""},
33+
para{"()[]{}"},
34+
ans{true},
35+
},
36+
question{
37+
para{"(]"},
38+
ans{false},
39+
},
40+
question{
41+
para{"({[]})"},
42+
ans{true},
43+
},
44+
question{
45+
para{"(){[({[]})]}"},
46+
ans{true},
47+
},
48+
question{
49+
para{"()"},
50+
ans{true},
51+
},
52+
question{
53+
para{"((([[[{{{"},
54+
ans{false},
55+
},
56+
question{
57+
para{"(())]]"},
58+
ans{false},
3559
},
36-
60+
3761
// 如需多个测试,可以复制上方元素。
3862
}
3963

4064
for _, q := range qs {
4165
a, p := q.ans, q.para
4266
fmt.Printf("~~%v~~\n", p)
4367

44-
ast.Equal(a.one, (p.one), "输入:%v", p)
68+
ast.Equal(a.one, isValid(p.one), "输入:%v", p)
4569
}
4670
}

0 commit comments

Comments
 (0)