题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
import java.util.*; public class Solution { /** * * @param s string字符串 * @return bool布尔型 */ public boolean isValid (String s) { // write code here Stack<Character> st = new Stack<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); switch (c) { case'(': case'[': case'{': st.push(c); break; case ')': if (st.empty() || st.peek() != '(') return false; st.pop(); break; case ']': if (st.empty() || st.peek() != '[') return false; st.pop(); break; case '}': if (st.empty() || st.peek() != '{') return false; st.pop(); break; } } return st.empty(); } }