我用的是两个栈,后面也加了对括号不匹配的判断,但就是死活就是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

相关推荐

03-28 16:43
佛山大学 Java
点赞 评论 收藏
分享
最喜欢秋天的火龙果很...:第一份工作一定要往大的去,工资低点没事。后面换工作会更好找,即使你去小公司,你也不可能不会换工作的。所以找大的去
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务