Daily Leetcode 20. Valid Parentheses

Easy 關於Stack

2023 Dec LC challenge

關於 stack 特性

  • 先進後出
  • Push(): Adds an element to the top of the stack.
  • Pop(): Removes and returns the top element of the stack.
  • Peek(): Returns the top element of the stack without removing it from the stack.
  • isEmpty(): Check whether the stack is empty.
  • push 和 pop 都發生在頂端

20. Valid Parentheses

Given a string s containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type.

想法

My Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Stack;

public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        for (char c : s.toCharArray()) {
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) return false;
                char top = stack.pop();
                if ((c == ')' && top != '(') ||
                    (c == '}' && top != '{') ||
                    (c == ']' && top != '[')) {
                    return false;
                }
            }
        }

        return stack.isEmpty();
    }
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus