题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return bool布尔型 */ // 解题思路,使用栈,存储操作符 // 注意边界情况,只输入[[[ function isValid(s) { if (s.length == 0 || s.length == 1) return false; const arr = s.split(""); const optMap = { ")": "(", "}": "{", "]": "[", }; const optStack = []; for (let item of arr) { if (["(", "{", "["].includes(item)) { optStack.push(item); } else { if (optMap[item]==optStack[optStack.length-1]) { optStack.pop(); }else{ return false; } } } if(optStack.length>0)return false; return true; } module.exports = { isValid: isValid, };