-
Notifications
You must be signed in to change notification settings - Fork 0
/
L_20_IsValid.java
61 lines (49 loc) · 1.46 KB
/
L_20_IsValid.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package me.mingshan.leetcode;
import java.util.Stack;
/**
* 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
*
* 有效字符串需满足:
*
* 左括号必须用相同类型的右括号闭合。
* 左括号必须以正确的顺序闭合。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/valid-parentheses
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class L_20_IsValid {
public static void main(String[] args) {
String s = "()[]{}";
System.out.println(isValid(s));
String s2 = "([{}])";
System.out.println(isValid(s2));
String s3 = "([]){}";
System.out.println(isValid(s3));
String s4 = "(]";
System.out.println(isValid(s4));
}
public static boolean isValid(String s) {
if (s == null || s.length() == 0) {
return false;
}
Stack<Character> stack = new Stack<>();
int n = s.length();
char[] chars = s.toCharArray();
for (int i = 0; i < n; i++) {
String value = String.valueOf(chars[i]);
if ("(".equals(value)) {
stack.push(')');
} else if ("{".equals(value)) {
stack.push('}');
} else if ("[".equals(value)) {
stack.push(']');
} else {
if (stack.isEmpty() || !value.equals(String.valueOf(stack.pop()))) {
return false;
}
}
}
return stack.isEmpty();
}
}