题解 | #括号序列#

括号序列

http://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2

/**
  * 
  * @param s string字符串 
  * @return bool布尔型
  */
function isValid( s ) {
    // write code here
    let temp = [];
    let ret = null;
    s.split('').forEach(item => {
        if(item === '['){
            temp.push(item)
        }
        else if(item === '('){
            temp.push(item)
        }
        else if(item === '{'){
            temp.push(item)
        }

        else if(item === ']'){
            if(temp[temp.length-1] === '['){
                temp.pop()
                ret =  true
            }else{
                ret = false
            }
        }
        else if(item === ')'){
            if(temp[temp.length-1] === '('){
                temp.pop()
                ret = true
            }else{
                ret = false
            }
        }
        else if(item === '}'){

            if(temp[temp.length-1] === '{'){
                temp.pop()
                ret = true
            }else{
                ret = false
            }
        }
    });
    if(temp.length !== 0){
        ret = false;
    }
    console.log(temp)
    return ret
}
module.exports = {
    isValid : isValid
};
全部评论

相关推荐

06-25 16:53
门头沟学院 Java
人力小鱼姐:简历可以直接用飞书模板 模拟面试可以试试ai,现在好多都还是免费阶段 像Sugar云面、多面鹅都不错,主要看面试后自己能不能复盘出有效信息
为了找工作你花了哪些钱?
点赞 评论 收藏
分享
想按时下班的我在等o...:我投测试也是这个情况,不知道咋办了
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务