-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid.java
More file actions
58 lines (53 loc) · 1.41 KB
/
Valid.java
File metadata and controls
58 lines (53 loc) · 1.41 KB
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
/**
* Created by Administrator on 2016/5/26.
*/
import java.util.Stack;
public class Valid {
public static void main(String args[]){
String ss =new String("[");
Valid vali = new Valid();
System.out.print(vali.isValid(ss));
}
boolean isValid(String s){
int i = 0;
Stack st = new Stack();
while(i<s.length()){
if(st.empty()){
st.push(s.charAt(i));
i++;
continue;
}
if((s.charAt(i)=='}')||(s.charAt(i)==']')||(s.charAt(i)==')'))
{
if(st.empty())
return false;
if(((char)(st.peek())=='[')&&(s.charAt(i)==']'))
{
st.pop();
i++;
continue;
}
if(((char)st.peek()=='(')&&(s.charAt(i)==')'))
{
st.pop();
i++;
continue;
}
if(((char)st.peek()=='{')&&(s.charAt(i)=='}'))
{
st.pop();
i++;
continue;
}
return false;
}else{
st.push(s.charAt(i));
i++;
continue;
}
}
if(!st.empty())
return false;
return true;
}
}