题解 | #括号序列#
括号序列
http://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
/**
*
* @param s string字符串
* @return bool布尔型
*/
function isValid( s ) {
const stack = [];
for(let i = 0; i < s.length; i++){
let item = s.charAt(i);
if(item === '(' || item === '{' || item === '['){
stack.push(item)
}else{
if(stack.length === 0) return false
else{
let o = stack.pop();
if(item === ')' && o !== '(') return false
if(item === '}' && o !== '{') return false
if(item === ']' && o !== '[') return false
}
}
}
return stack.length === 0 ? true:false
}
module.exports = {
isValid : isValid
};