-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidParentheses.java
50 lines (44 loc) · 1.33 KB
/
ValidParentheses.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
package easy;
import java.util.Stack;
/**
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']',
* determine if the input string is valid.
* The brackets must close in the correct order,
* "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
*
* 解决思路:
* 经典出入栈应用问题
* 判断左右括号
* 利用栈结构进行入栈和出栈对比
*
* Created by second on 2017/7/17.
*/
public class ValidParentheses {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
boolean boo = true;
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
//左括号入栈
if (isLeft(c)){
stack.push(c);
}else {
//右括号出栈对比
if (stack.isEmpty() || !match(stack.pop(), c)){
boo = false;
}
}
}
if (!stack.isEmpty()) return false; //遍历结束正确情况下栈应该为空
return boo;
}
private boolean isLeft(char c){
return '(' == c || '{' == c || '[' == c;
}
private boolean match(char a, char b){
if (a == '(') return b == ')';
if (a == '{') return b == '}';
if (a == '[') return b == ']';
return false;
}
}