Skip to content

Commit 51ac836

Browse files
committed
[E:55/531, M:52/969, H:6/386] add No: 20 Valid Parentheses
1 parent b079ce1 commit 51ac836

File tree

13 files changed

+418
-1
lines changed

13 files changed

+418
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## [有效的括号](https://leetcode-cn.com/problems/valid-parentheses/)
2+
3+
给定一个只包括 `'('``')'``'{'``'}'``'['``']'` 的字符串,判断字符串是否有效。
4+
5+
有效字符串需满足:
6+
7+
1. 左括号必须用相同类型的右括号闭合。
8+
2. 左括号必须以正确的顺序闭合。
9+
10+
注意空字符串可被认为是有效字符串。
11+
12+
**示例 1:**
13+
14+
`**输入:** "()"
15+
**输出:** true
16+
`
17+
18+
**示例 2:**
19+
20+
`**输入:** "()[]{}"
21+
**输出:** true
22+
`
23+
24+
**示例 3:**
25+
26+
`**输入:** "(]"
27+
**输出:** false
28+
`
29+
30+
**示例 4:**
31+
32+
`**输入:** "([)]"
33+
**输出:** false
34+
`
35+
36+
**示例 5:**
37+
38+
`**输入:** "{[]}"
39+
**输出:** true`
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/20/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input string
18+
want bool
19+
}{
20+
{
21+
name: "test-()",
22+
input: "()",
23+
want: true,
24+
},
25+
{
26+
name: "test-()[]{}",
27+
input: "()[]{}",
28+
want: true,
29+
},
30+
{
31+
name: "test-(]",
32+
input: "(]",
33+
want: false,
34+
},
35+
{
36+
name: "test-([)]",
37+
input: "([)]",
38+
want: false,
39+
},
40+
{
41+
name: "test-{[]}",
42+
input: "{[]}",
43+
want: true,
44+
},
45+
}
46+
47+
testLog := leet.NewTestLog(len(tests))
48+
defer testLog.Render()
49+
50+
timeoutDuration := time.Second * 2
51+
52+
for idx, test := range tests {
53+
// 超时检测
54+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
55+
solution.Export(test.input)
56+
cancel()
57+
})
58+
59+
if timeout {
60+
testLog.Fail(idx+1, test.name, "timeout")
61+
continue
62+
}
63+
64+
got := solution.Export(test.input)
65+
if !reflect.DeepEqual(test.want, got) {
66+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
67+
continue
68+
}
69+
70+
testLog.Pass(idx+1, test.name)
71+
}
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(s string) bool {
4+
return isValid(s)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func isValid(s string) bool {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(s string) bool {
4+
return isValid(s)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func isValid(s string) bool {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## [有效的括号](https://leetcode-cn.com/problems/valid-parentheses/)
2+
3+
给定一个只包括 `'('``')'``'{'``'}'``'['``']'` 的字符串,判断字符串是否有效。
4+
5+
有效字符串需满足:
6+
7+
1. 左括号必须用相同类型的右括号闭合。
8+
2. 左括号必须以正确的顺序闭合。
9+
10+
注意空字符串可被认为是有效字符串。
11+
12+
**示例 1:**
13+
14+
`**输入:** "()"
15+
**输出:** true
16+
`
17+
18+
**示例 2:**
19+
20+
`**输入:** "()[]{}"
21+
**输出:** true
22+
`
23+
24+
**示例 3:**
25+
26+
`**输入:** "(]"
27+
**输出:** false
28+
`
29+
30+
**示例 4:**
31+
32+
`**输入:** "([)]"
33+
**输出:** false
34+
`
35+
36+
**示例 5:**
37+
38+
`**输入:** "{[]}"
39+
**输出:** true`
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/20/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input string
18+
want bool
19+
}{
20+
{
21+
name: "test-()",
22+
input: "()",
23+
want: true,
24+
},
25+
{
26+
name: "test-()[]{}",
27+
input: "()[]{}",
28+
want: true,
29+
},
30+
{
31+
name: "test-(]",
32+
input: "(]",
33+
want: false,
34+
},
35+
{
36+
name: "test-([)]",
37+
input: "([)]",
38+
want: false,
39+
},
40+
{
41+
name: "test-{[]}",
42+
input: "{[]}",
43+
want: true,
44+
},
45+
}
46+
47+
testLog := leet.NewTestLog(len(tests))
48+
defer testLog.Render()
49+
50+
timeoutDuration := time.Second * 2
51+
52+
for idx, test := range tests {
53+
// 超时检测
54+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
55+
solution.Export(test.input)
56+
cancel()
57+
})
58+
59+
if timeout {
60+
testLog.Fail(idx+1, test.name, "timeout")
61+
continue
62+
}
63+
64+
got := solution.Export(test.input)
65+
if !reflect.DeepEqual(test.want, got) {
66+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
67+
continue
68+
}
69+
70+
testLog.Pass(idx+1, test.name)
71+
}
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(s string) bool {
4+
return isValid(s)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func isValid(s string) bool {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(s string) bool {
4+
return isValid(s)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func isValid(s string) bool {
13+
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## [有效的括号](https://leetcode-cn.com/problems/valid-parentheses/)
2+
3+
给定一个只包括 `'('``')'``'{'``'}'``'['``']'` 的字符串,判断字符串是否有效。
4+
5+
有效字符串需满足:
6+
7+
1. 左括号必须用相同类型的右括号闭合。
8+
2. 左括号必须以正确的顺序闭合。
9+
10+
注意空字符串可被认为是有效字符串。
11+
12+
**示例 1:**
13+
14+
`**输入:** "()"
15+
**输出:** true
16+
`
17+
18+
**示例 2:**
19+
20+
`**输入:** "()[]{}"
21+
**输出:** true
22+
`
23+
24+
**示例 3:**
25+
26+
`**输入:** "(]"
27+
**输出:** false
28+
`
29+
30+
**示例 4:**
31+
32+
`**输入:** "([)]"
33+
**输出:** false
34+
`
35+
36+
**示例 5:**
37+
38+
`**输入:** "{[]}"
39+
**输出:** true`

0 commit comments

Comments
 (0)