import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return bool布尔型 */ public boolean isValid (String s) { Stack<Character> st = new Stack<>(); for (char c : s.toCharArray()) { if (c == '[' || c == '(' || c == '{') { st.push(c); } else if (c == ']') { if (st.isEmpty() || st.peek() != '[') { return false; } else { st.pop(); } } else if (c == ')') { if (st.isEmpty() || st.peek() != '(') { return false; } else { st.pop(); } } else if (c == '}') { if (st.isEmpty() || st.peek() != '{') { return false; } else { st.pop(); } } } // StringBuilder sb = new StringBuilder(); // while(!st.isEmpty()){ // sb.append(st.pop()); // } // System.out.println(sb.reverse()); return st.isEmpty(); } }