题目描述
https://leetcode-cn.com/problems/valid-parentheses/
代码
/*** @param {string} s* @return {boolean}*/var isValid = function (s) {/** 若长度为奇数,括号不可能全部闭合,直接false */if (s.length % 2 === 1) {return false}const stack = []const map = new Mapmap.set('(', ')')map.set('{', '}')map.set('[', ']')const len = s.lengthfor (let i = 0; i < s.length; i++) {const c = s[i]if (map.has(c)) {stack.push(c)} else {const popItem = stack[stack.length - 1]if (map.get(popItem) === c) {stack.pop()} else {return false}}}return stack.length === 0}
