-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidParentheses.java
42 lines (42 loc) · 1.07 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
/********************************************************
> File Name:20ValidParentheses.java
> Auther: ihochang
> Mail: [email protected]
> Created Time: Fri Jan 8 01:26:57 2016
*********************************************************/
import java.util.Stack;
public class ValidParentheses {
public boolean isValid(String s) {
Stack<Character> pa = new Stack<Character>();
int len = s.length();
char temp;
char patemp=0;
if (len%2==1){
return false;
}
for (int i = 0; i<len; i++) {
temp = s.charAt(i);
if (pa.empty() != true) {
patemp = pa.peek();
}
if (temp == '(' || temp == '[' || temp == '{') {
pa.push(temp);
} else if((int)temp-(int)patemp>3){
return false;
} else {
pa.pop();
}
}
if (pa.empty() == true) {
return true;
} else
return false;
}
public static void main(String[] args) {
ValidParentheses so = new ValidParentheses();
System.out.println(so.isValid("{}[]()"));
System.out.println(so.isValid(""));
System.out.println(so.isValid(")("));
System.out.println(so.isValid("([)]"));
}
}