forked from luox78/go-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parentheses_test.go
101 lines (81 loc) · 1.86 KB
/
parentheses_test.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package stack_test
import (
"testing"
"github.com/wenjiax/go-algorithms/stack"
)
func TestVaildParentheses(t *testing.T) {
// 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
// 有效字符串需满足:
// 左括号必须用相同类型的右括号闭合。
// 左括号必须以正确的顺序闭合。
// 注意空字符串可被认为是有效字符串。
// 示例 1:
// 输入: "()"
// 输出: true
t.Log(vaildParentheses("()"))
t.Log(isVaild("()"))
// 示例 2:
// 输入: "()[]{}"
// 输出: true
t.Log(vaildParentheses("()[]{}"))
t.Log(isVaild("()[]{}"))
// 示例 3:
// 输入: "(]"
// 输出: false
t.Log(vaildParentheses("(]"))
t.Log(isVaild("(]"))
// 示例 4:
// 输入: "([)]"
// 输出: false
t.Log(vaildParentheses("([)]"))
t.Log(isVaild("([)]"))
// 示例 5:
// 输入: "{[]}"
// 输出: true
t.Log(vaildParentheses("{[]}"))
t.Log(isVaild("{[]}"))
}
// 有效的括号
func vaildParentheses(str string) bool {
// 记录括号匹配映射
mappings := map[string]string{
")": "(",
"]": "[",
"}": "{",
}
// 利用栈特性进行匹配
stack := stack.NewArrayStack(len(str))
for _, c := range str {
s := string(c)
if ms, ok := mappings[s]; !ok {
// 未找到映射, 存入栈
stack.Push(s)
} else {
// 从栈中取出一个字符串进行比较
if ss := stack.Pop(); ms != ss {
return false
}
}
}
return stack.Len() == 0
}
func isVaild(str string) bool {
mappings := map[string]string{")": "(", "]": "[", "}": "{"}
var stack []string
for _, c := range str {
s := string(c)
if ms, ok := mappings[s]; !ok {
stack = append(stack, s)
} else {
if len(stack) == 0 {
return false
} else {
if ss := stack[len(stack)-1]; ss != ms {
return false
}
stack = stack[:len(stack)-1]
}
}
}
return len(stack) == 0
}