-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
47 lines (42 loc) · 1.5 KB
/
Solution.java
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
package ds.stack.leetcode20;
import java.util.Stack;
/**
* 有效的括号
* LeetCode 20 https://leetcode-cn.com/problems/valid-parentheses/
*
* @author yangyi 2021年07月09日19:20:06
*/
public class Solution {
public boolean isValid(String s) {
Stack<String> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push(")");
} else if (s.charAt(i) == ('{')) {
stack.push("}");
} else if (s.charAt(i) == ('[')) {
stack.push("]");
} else if (stack.isEmpty()) {
return false;
} else if (!stack.peek().equals(String.valueOf(s.charAt(i)))) {
return false;
} else {
stack.pop();
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
String one = "()";
String two = "()[]";
String three = "([)]";
String four = "(((([]))";
String five = "]][[";
Solution solution = new Solution();
System.out.println(one + (solution.isValid(one) ? "有效" : "无效"));
System.out.println(two + (solution.isValid(two) ? "有效" : "无效"));
System.out.println(three + (solution.isValid(three) ? "有效" : "无效"));
System.out.println(four + (solution.isValid(four) ? "有效" : "无效"));
System.out.println(five + (solution.isValid(five) ? "有效" : "无效"));
}
}