验证括号闭合有效性
作者:
Volcaner | 来源:发表于
2021-03-16 16:06 被阅读0次// "{{{[[[((()))]]]}}}(())"
/**
*
* @param s string字符串
* @return bool布尔型
*/
function isValid( s ) {
// write code here
const list = {
'(': ')',
'[': ']',
'{': '}'
};
let stack = [];
const _listKeys = Object.keys(list);
// stack.push(s.charAt(0));
for(let i = 0; i < s.length; i++) {
if(_listKeys.indexOf(s.charAt(i)) > -1) stack.push(s.charAt(i));
else {
if(stack.length > 0 && list[stack[stack.length - 1]] === s.charAt(i))
stack.pop();
else return false;
}
}
return stack.length === 0;
}
module.exports = {
isValid : isValid
};
本文标题:验证括号闭合有效性
本文链接:https://www.haomeiwen.com/subject/scudcltx.html
网友评论