标签:tac symbol public false 获取 push stack ret print
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);
}
}
标签:tac symbol public false 获取 push stack ret print
原文地址:https://www.cnblogs.com/huiandong/p/10225946.html