Difficult:Easy
題目
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.
翻譯
給定一個僅包含字符 ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ 和 ‘]’ 的字符串 s,確定輸入字符串是否有效。
輸入字符串在以下情況下有效:
開括號必須用相同類型的括號閉合。
開括號必須以正確的順序閉合。
範例
Example 1
1 | Input: s = "()" |
Example 2
1 | Input: s = "()[]{}" |
Example 3
1 | Input: s = "(]" |
解題思路
1.檢查是否為前墜的字元,是的話放入堆疊中
2.不是前墜的字元,跟堆疊中最後一個字比對
Solution
Code 1 :
1 | var isValid = function(s){ |