20. Valid Parentheses
自認寫的有點鳥... 再想想看
題目原文
Given a string 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.
Note that an empty string is also considered valid.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
解題思路
善用stack。
遇到左括弧先push到Stack裡,遇到右括弧再從Stack pop出左括弧。
pop出來的左括弧是否對應相對的右括弧。
最後,判斷Stack是否為空。
程式解答
Last updated
Was this helpful?