云開發(fā)核心知識+ 音樂項目實戰(zhàn)-coderwhy2022系統(tǒng)課
? public static boolean isValid(String string) {
? ? ? ?Map<Character, Character> bracketMap = new HashMap<>();
? ? ? ?bracketMap.put('[', ']');
? ? ? ?bracketMap.put('(', ')');
? ? ? ?bracketMap.put('{', '}');
? ? ? ?if (string == null || string.isEmpty()) {
? ? ? ? ? ?return true;
? ? ? ?}
? ? ? ?Deque<Character> stack = new ArrayDeque<>();
? ? ? ?char[] chars = string.toCharArray();
? ? ? ?for (char ch : chars) {
? ? ? ? ? ?if ("[{(".indexOf(ch) >= 0) {
? ? ? ? ? ? ? ?stack.push(ch);
? ? ? ? ? ? ? ?continue;
? ? ? ? ? ?}
? ? ? ? ? ?Character head = stack.peek();
? ? ? ? ? ?if ("]})".indexOf(ch) >= 0) {
? ? ? ? ? ? ? ?if (head != null && ch == bracketMap.get(head)) {
? ? ? ? ? ? ? ? ? ?stack.pop();
? ? ? ? ? ? ? ? ? ?continue;
? ? ? ? ? ? ? ?} else {
? ? ? ? ? ? ? ? ? ?return false;
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}