mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 20
This commit is contained in:
57
Algorithms/20.Valid Parentheses/README.md
Normal file
57
Algorithms/20.Valid Parentheses/README.md
Normal file
@@ -0,0 +1,57 @@
|
||||
# [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。
|
||||
21
Algorithms/20.Valid Parentheses/Valid Parentheses.go
Normal file
21
Algorithms/20.Valid Parentheses/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
|
||||
}
|
||||
69
Algorithms/20.Valid Parentheses/Valid Parentheses_test.go
Normal file
69
Algorithms/20.Valid Parentheses/Valid Parentheses_test.go
Normal file
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,7 @@
|
||||
|17|[Letter Combinations of a Phone Number](./Algorithms/17.letter-combinations-of-a-phone-number)|36%|Medium||
|
||||
|18|[4Sum](./Algorithms/18.4sum)|27%|Medium||
|
||||
|19|[Remove Nth Node From End of List](./Algorithms/19.remove-nth-node-from-end-of-list)|33%|Medium||
|
||||
|20|[Valid Parentheses](./Algorithms/20.valid-parentheses)|33%|Easy||
|
||||
|20|[Valid Parentheses](./Algorithms/20.Valid-Parentheses)|33%|Easy||
|
||||
|21|[Merge Two Sorted Lists](./Algorithms/21.merge-two-sorted-lists)|41%|Easy||
|
||||
|22|[Generate Parentheses](./Algorithms/22.generate-parentheses)|48%|Medium|[❤](https://leetcode.com/list/oussv5j)|
|
||||
|23|[Merge k Sorted Lists](./Algorithms/23.merge-k-sorted-lists)|28%|Hard||
|
||||
@@ -178,7 +178,7 @@
|
||||
|122|[Best Time to Buy and Sell Stock II](./Algorithms/122.best-time-to-buy-and-sell-stock-ii)|47%|Easy||
|
||||
|123|[Best Time to Buy and Sell Stock III](./Algorithms/123.best-time-to-buy-and-sell-stock-iii)|30%|Hard||
|
||||
|124|[Binary Tree Maximum Path Sum](./Algorithms/124.binary-tree-maximum-path-sum)|27%|Hard|[❤](https://leetcode.com/list/oussv5j)|
|
||||
|125|[Valid Palindrome](./Algorithms/125.Valid-Palindrome)|26%|Easy||
|
||||
|125|[Valid Palindrome](./Algorithms/125.Valid-Palindrome)|27.0%|Easy||
|
||||
|126|[Word Ladder II](./Algorithms/126.word-ladder-ii)|14%|Hard|[❤](https://leetcode.com/list/oussv5j)|
|
||||
|127|[Word Ladder](./Algorithms/127.word-ladder)|20%|Medium|[❤](https://leetcode.com/list/oussv5j)|
|
||||
|128|[Longest Consecutive Sequence](./Algorithms/128.longest-consecutive-sequence)|38%|Hard||
|
||||
|
||||
Reference in New Issue
Block a user