有效的括号

75 阅读1分钟

const isValid = (s) => { if (s.length % 2 === 1) return false const stack = [] const map = new Map() map.set('(',')') map.set('{','}') map.set('[', ']') for (let i = 0; i < s.length; i++){ const c = s[i] if (map.has(c)) { stack.push(c) } else { const t = stack[stack.length-1] if (map.get(t) === c) { stack.pop() } else { return false } } } return stack.length===0 } console.log(isValid('()'))