我用的是两个栈,后面也加了对括号不匹配的判断,但就是死活就是57 static String resolve(String expr) {         if(expr==null)             return null;         Stack<Integer> stack1=new Stack<>();         Stack<Integer> stack2=new Stack<>();         for(int i=0;i<expr.length();i++){             char c=expr.charAt(i);             if(c!=')'){                 stack1.add((int)c);             }else{                 if(!stack1.isEmpty()){                     while(stack1.peek()!=(int)'(')                         stack2.add(stack1.pop());                     stack1.pop();                     while(!stack2.isEmpty()){                         stack1.add(stack2.pop());                     }                 }             }         }         String rs="";         boolean match=true;         while(!stack1.isEmpty()){             if(stack1.peek()=='('||stack1.peek()==')'){                 match=false;                 break;             }             rs+=stack1.pop();         }         return match?rs:"";     }
点赞 3

相关推荐

牛客热帖

牛客网
牛客企业服务