stackApp符号匹配

public class SymbolMatch {
public static boolean isMatch(String s){
//[{()}]
ArrayStack<Character> arrayStack = new ArrayStack<Character>();
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(c=='[' || c=='{' || c=='('){
arrayStack.push(c);//放入栈中[{(
}else{
if(arrayStack.isEmpty()){
return false;
}
Character pop = arrayStack.pop();//获取栈顶元素
if(c==']' && pop!='[') {//如果当前字符和栈顶的字符匹配,
return false;
}
if(c=='}' && pop!='{') {
return false;
}
if(c==')' && pop!='('){
return false;
}
}
}
return arrayStack.isEmpty();
}

public static void main(String[] args) {
boolean match = isMatch("{(])}");
System.out.println(match);
}

}
posted @ 2019-01-05 20:12  uuhh  阅读(273)  评论(0编辑  收藏  举报