用栈记录左边符号,遇到右边符号出栈
map 设置()【】{},set左边符号,get拿到的右边符号
栈顶元素与map拿到的右边符号不等时,循环结束,括号不匹配
栈空,括号匹配
function IsValidExp(line) { let flag = false; let str = line.replaceAll('"', ""); const map = new Map(); map.set("(", ")"); map.set("[", "]"); map.set("{", "}"); const stack = []; for (let i = 0; i < str.length; i++) { if (map.has(str[i])) { stack.push(str[i]); } else { let popItem = stack.pop(); if (str[i] != map.get(popItem)) { return flag } } } if (stack.length == 0) { flag = true; } return flag } module.exports = { IsValidExp: IsValidExp, };