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