题解 | #有效括号序列#

有效括号序列

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

/**
  * 
  * @param s string字符串 
  * @return bool布尔型
  */
function isValid( s ) {
    // write code here
      let arr = s.split('')
      if(arr.length % 2 !== 0)return false
  let temp = []
  for(let i = 0;i<arr.length;i++){
    if(arr[i] === '('){
      temp.push(arr[i])
    }
    if(arr[i] === '['){
      temp.push(arr[i])
    }
    if(arr[i] === '{'){
      temp.push(arr[i])
    }
    if(arr[i] === ')'&& temp[temp.length-1] === '('){
      temp.pop()
    }
    if(arr[i] === ']'&& temp[temp.length-1] === '['){
      temp.pop()
    }
    if(arr[i] === '}'&& temp[temp.length-1] === '{'){
      temp.pop()
    }
  }
  if(temp.length === 0){
    return true
  }
  return false
}
module.exports = {
    isValid : isValid
};
全部评论

相关推荐

牛客37185681...:马德,我感觉这是我面过最恶心的公司,一面是两个女hr,说什么实习前几个月属于试用期,试用期过了才能转成正式实习生,我***笑了,问待遇就是不说,问能不能接受全栈,沙币公司
如果可以选,你最想去哪家...
点赞 评论 收藏
分享
自来熟的放鸽子能手面...:这个不一定,找hr跟进一下
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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