import java.util.*; public class Solution { /** * * @param s string字符串 * @return bool布尔型 */ public boolean isValid (String s) { // write code here if (s == null || s.length() < 2) { return false; } char[] arr = s.toCharArray(); Stack<Character> stack = new Stack<>(); for (int i = 0; i < arr.length; i++) { if (!stack.isEmpty() && ((stack.peek() == '(' && arr[i] == ')') || (stack.peek() == '[' && arr[i] == ']') || (stack.peek() == '{' && arr[i] == '}'))) { stack.pop(); } else { stack.push(arr[i]); } } return stack.isEmpty(); } }