stackApp符号匹配

Posted uuhh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}

}
































以上是关于stackApp符号匹配的主要内容,如果未能解决你的问题,请参考以下文章