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