Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Note that an empty string is also considered valid.
Example 1:
Input: "()"Output: true
Example 2:
Input: "()[]{}"Output: true
Example 3:
Input: "(]"Output: false
Example 4:
Input: "([)]"Output: false
Example 5:
Input: "{[]}"Output: true
考察栈的使用。
/*** @param {string} s* @return {boolean}*/var isValid = function (s) {const obj = {'(': ')','{': '}','[': ']',}const cacheArr = []for (let i = 0; i < s.length; i++) {if (Object.keys(obj).includes(s[i])) {cacheArr.push(s[i])} else {const pick = cacheArr.pop()if (obj[pick] !== s[i]) {return false}}}return cacheArr.length === 0}