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t5kcqU+z/ji+XOMvv7eZnaLGE/cKD8ZMO5C883DY/pRLV70xvDq+GuXK1K7B/Ud8tUDkAZGe/213jZ5BX6/1uV0gsg81PqaCGhx713QKVA8A12O/2281vrrl1td5ufq42UeaNZfj5KRXU8NCjvk+gcgAAAABQJlA5AAAAACgTqBwAAAAAlAlUDgAAAADKBCoHAAAAAGUClQMAAACAMoHKAQAAAECZ/A+2rcNQDQOswwAAAABJRU5ErkJggg==" alt="" />
思路:用Stack基本操作完成。
要检测输入字符是否满足这个条件,一个非常合适的数据结构是stack,后进先出的特征正好满足检测的需求。在检测的时候,每次检查一个字符,如果是左括号,就入栈,如果是右括号,并且右括号和当前栈顶符号是左右配对的,那么就弹出栈顶并且进行下一次检测,如果不满足上面两种情况,就说明检查到了一个非法字符,返回false.
public class Solution {
public boolean isValid(String s) {
if(s.length()==0){
return true;
}
Stack<Character> st=new Stack<Character>();
st.push(s.charAt(0));
for(int i=1;i<s.length();i++){
if(!st.empty()&&isMatch(st.peek(),s.charAt(i))){
st.pop();
}else
st.push(s.charAt(i));
}
if(st.empty()){
return true;
}
return false;
} public static boolean isMatch(char s,char p){
if((s=='('&&p==')')||(s=='['&&p==']')||(s=='{'&&p=='}')){
return true;
}else
return false;
}
}