import java.util.*; public class Solution { /** * * @param s string字符串 * @return bool布尔型 */ public boolean isValid (String s) { // write code here Stack<Character> stack = new Stack<>(); String data = s; if(data == null){ return false; } for(int i=0;i<data.length();i++){ if(isLeft(data.charAt(i))){ stack.push(data.charAt(i)); }else if(isRight(data.charAt(i))){ if(stack.isEmpty()){ return false; } if(needPop(stack.peek(),data.charAt(i))){ stack.pop(); } } } if(stack.isEmpty()){ return true; }else{ return false; } } private static boolean needPop(Character peek, char charAt) { boolean result = false; switch(charAt){ case ')': if(peek == '('){ result = true; } break; case ']': if(peek == '['){ result = true; } break; default: if(peek == '{'){ result = true; } } return result; } private static boolean isRight(char charAt) { if(charAt==']' || charAt == '}' || charAt == ')'){ return true; }else{ return false; } } private static boolean isLeft(char charAt) { if(charAt=='[' || charAt == '{' || charAt == '('){ return true; }else{ return false; } } }