mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
21
leetcode/0020.Valid-Parentheses/20. Valid Parentheses.go
Normal file
21
leetcode/0020.Valid-Parentheses/20. Valid Parentheses.go
Normal file
@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
func isValid(s string) bool {
|
||||
// 空字符串直接返回 true
|
||||
if len(s) == 0 {
|
||||
return true
|
||||
}
|
||||
stack := make([]rune, 0)
|
||||
for _, v := range s {
|
||||
if (v == '[') || (v == '(') || (v == '{') {
|
||||
stack = append(stack, v)
|
||||
} else if ((v == ']') && len(stack) > 0 && stack[len(stack)-1] == '[') ||
|
||||
((v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(') ||
|
||||
((v == '}') && len(stack) > 0 && stack[len(stack)-1] == '{') {
|
||||
stack = stack[:len(stack)-1]
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return len(stack) == 0
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question20 struct {
|
||||
para20
|
||||
ans20
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para20 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans20 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem20(t *testing.T) {
|
||||
|
||||
qs := []question20{
|
||||
|
||||
question20{
|
||||
para20{"()[]{}"},
|
||||
ans20{true},
|
||||
},
|
||||
question20{
|
||||
para20{"(]"},
|
||||
ans20{false},
|
||||
},
|
||||
question20{
|
||||
para20{"({[]})"},
|
||||
ans20{true},
|
||||
},
|
||||
question20{
|
||||
para20{"(){[({[]})]}"},
|
||||
ans20{true},
|
||||
},
|
||||
question20{
|
||||
para20{"((([[[{{{"},
|
||||
ans20{false},
|
||||
},
|
||||
question20{
|
||||
para20{"(())]]"},
|
||||
ans20{false},
|
||||
},
|
||||
question20{
|
||||
para20{""},
|
||||
ans20{true},
|
||||
},
|
||||
question20{
|
||||
para20{"["},
|
||||
ans20{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 20------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans20, q.para20
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, isValid(p.one))
|
||||
}
|
||||
}
|
59
leetcode/0020.Valid-Parentheses/README.md
Normal file
59
leetcode/0020.Valid-Parentheses/README.md
Normal file
@ -0,0 +1,59 @@
|
||||
# [20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
|
||||
|
||||
An input string is valid if:
|
||||
|
||||
Open brackets must be closed by the same type of brackets.
|
||||
Open brackets must be closed in the correct order.
|
||||
Note that an empty string is also considered valid.
|
||||
|
||||
Example 1:
|
||||
|
||||
```
|
||||
Input: "()"
|
||||
Output: true
|
||||
|
||||
```
|
||||
|
||||
|
||||
Example 2:
|
||||
|
||||
```
|
||||
Input: "()[]{}"
|
||||
Output: true
|
||||
|
||||
```
|
||||
|
||||
Example 3:
|
||||
|
||||
```
|
||||
Input: "(]"
|
||||
Output: false
|
||||
```
|
||||
|
||||
Example 4:
|
||||
|
||||
```
|
||||
Input: "([)]"
|
||||
Output: false
|
||||
```
|
||||
|
||||
Example 5:
|
||||
|
||||
```
|
||||
Input: "{[]}"
|
||||
Output: true
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
括号匹配问题。
|
||||
|
||||
## 解题思路
|
||||
|
||||
遇到左括号就进栈push,遇到右括号并且栈顶为与之对应的左括号,就把栈顶元素出栈。最后看栈里面还有没有其他元素,如果为空,即匹配。
|
||||
|
||||
需要注意,空字符串是满足括号匹配的,即输出 true。
|
Reference in New Issue
Block a user