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.
Source
public class Solution {
public boolean isValid(String s) {
char a,b;
Stack<Character> st = new Stack<Character>(); //*****
for(int i = 0; i < s.length(); i++){
a = s.charAt(i);
if(a == '(' || a == '{' || a == '[')
st.push(a);
else if(a == ')' || a == '}' || a == ']'){
if(st.size() == 0) return false; //在pop前判空,否则RE
b = st.pop(); //pop没有参数
if(b == '(' && a == ')') continue;
if(b == '{' && a == '}') continue;
if(b == '[' && a == ']') continue;
return false;
}
}
if(st.size() == 0) return true; //**********
else return false;
}
}