public static String reverse(String str){ Stack<Character> stack = new Stack<>(); String s = ""; int len = str.length(); for(int i=0;i<len;i++){ char c = str.charAt(i); if(c!=')'){ stack.push(c); }else{ if(stack.isEmpty()){ return ""; } s = ""; while(!stack.isEmpty()&&stack.peek()!='('){ s+=stack.pop(); } if(stack.isEmpty()||stack.pop()!='('){ return ""; } int l = s.length(); for(int j=0;j<l;j++){ stack.push(s.charAt(j)); } } } s = ""; while(!stack.isEmpty()){ //注意不是s = s + stack.pop(); s = stack.pop()+ s; } return s; }
点赞 2

相关推荐

投递蚂蚁集团等公司10个岗位
点赞 评论 收藏
分享
亲切的00后在笔试:我也遇到了,所以我早他一步查看图片
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务