

解法一:使用栈
function isValid(s: string): boolean {const stack = []const map = new Map([[')','('],['}', '{'],[']', '[']])for(const c of s) {// 将左括号推入栈顶if (!map.has(c)) {stack.push(c)} else {// 如果为右括号,则取出栈顶,判断是否匹配if (stack.pop() !== map.get(c)) {return false}}}return stack.length === 0};
