import java.util.*; public class Solution { /** * * @param s string字符串 * @return bool布尔型 */ public boolean isValid (String s) { // write code here //利用栈实现 if("".equals(s)){ return false; } Stack<Character> stack = new Stack<>(); for(int i =0;i<s.length();i++){ char next = s.charAt(i); //遇到左括号就入栈 if(next == '(' || next == '[' || next == '{'){ stack.push(next); } else { //右括号就出栈比对 if(stack.isEmpty()){ return false; } else { char cur = stack.pop(); if((cur == '(' && next == ')') || (cur == '[' && next == ']') || (cur == '{' && next == '}')){ continue; } else { return false; } } } } if(stack.isEmpty()){ return true; } else { return false; } } }