File tree 3 files changed +121
-0
lines changed
solution/020.Valid Parentheses
3 files changed +121
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ Complete solutions to Leetcode problems, updated daily.
20
20
| 007 | [ Reverse Integer] ( https://github.com/yanglbme/leetcode/tree/master/solution/007.Reverse%20Integer ) | ` Math ` |
21
21
| 013 | [ Roman to Integer] ( https://github.com/yanglbme/leetcode/tree/master/solution/013.Roman%20to%20Integer ) | ` Math ` , ` String ` |
22
22
| 014 | [ Longest Common Prefix] ( https://github.com/yanglbme/leetcode/tree/master/solution/014.Longest%20Common%20Prefix ) | ` String ` |
23
+ | 020 | [ Valid Parentheses] ( https://github.com/yanglbme/leetcode/tree/master/solution/020.Valid%20Parentheses ) | ` String ` , ` Stack ` |
23
24
| 021 | [ Merge Two Sorted Lists] ( https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists ) | ` Linked List ` |
24
25
| 053 | [ Maximum Subarray] ( https://github.com/yanglbme/leetcode/tree/master/solution/053.Maximum%20Subarray ) | ` Array ` , ` Divide and Conquer ` , ` Dynamic Programming ` |
25
26
| 070 | [ Climbing Stairs] ( https://github.com/yanglbme/leetcode/tree/master/solution/070.Climbing%20Stairs ) | ` Dynamic Programming ` |
Original file line number Diff line number Diff line change
1
+ ## 有效的括号
2
+ ### 题目描述
3
+
4
+ 给定一个只包括 '(',')','{','}','[ ','] ' 的字符串,判断字符串是否有效。
5
+
6
+ 有效字符串需满足:
7
+
8
+ 左括号必须用相同类型的右括号闭合。
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
40
+ ```
41
+
42
+ ### 解法
43
+ 遍历 string,遇到左括号,压入栈中;遇到右括号,从栈中弹出元素,元素不存在或者元素与该右括号不匹配,返回 false。遍历结束,栈为空则返回 true,否则返回 false。
44
+
45
+ ``` java
46
+ class Solution {
47
+ public boolean isValid (String s ) {
48
+ if (s == null || s == " " ) {
49
+ return true ;
50
+ }
51
+ char [] chars = s. toCharArray();
52
+ int n = chars. length;
53
+
54
+ Stack<Character > stack = new Stack<> ();
55
+
56
+ for (int i = 0 ; i < n; ++ i) {
57
+ char a = chars[i];
58
+ if (isLeft(a)) {
59
+ stack. push(a);
60
+ } else {
61
+ if (stack. isEmpty() || ! isMatch(stack. pop(), a)) {
62
+ return false ;
63
+ }
64
+ }
65
+ }
66
+
67
+ return stack. isEmpty();
68
+
69
+ }
70
+
71
+ private boolean isMatch (char a , char b ) {
72
+ return (a == ' (' && b == ' )' ) || (a == ' [' && b == ' ]' ) || (a == ' {' && b == ' }' );
73
+ }
74
+
75
+ private boolean isLeft (char a ) {
76
+ return a == ' (' || a == ' [' || a == ' {' ;
77
+ }
78
+
79
+ private boolean isRight (char a ) {
80
+ return a == ' )' || a == ' ]' || a == ' }' ;
81
+ }
82
+ }
83
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isValid (String s ) {
3
+ if (s == null || s == "" ) {
4
+ return true ;
5
+ }
6
+ char [] chars = s .toCharArray ();
7
+ int n = chars .length ;
8
+
9
+ Stack <Character > stack = new Stack <>();
10
+
11
+ for (int i = 0 ; i < n ; ++i ) {
12
+ char a = chars [i ];
13
+ if (isLeft (a )) {
14
+ stack .push (a );
15
+ } else {
16
+ if (stack .isEmpty () || !isMatch (stack .pop (), a )) {
17
+ return false ;
18
+ }
19
+ }
20
+ }
21
+
22
+ return stack .isEmpty ();
23
+
24
+ }
25
+
26
+ private boolean isMatch (char a , char b ) {
27
+ return (a == '(' && b == ')' ) || (a == '[' && b == ']' ) || (a == '{' && b == '}' );
28
+ }
29
+
30
+ private boolean isLeft (char a ) {
31
+ return a == '(' || a == '[' || a == '{' ;
32
+ }
33
+
34
+ private boolean isRight (char a ) {
35
+ return a == ')' || a == ']' || a == '}' ;
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments