题解 | #有效括号序列#

有效括号序列

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
};
全部评论

相关推荐

昨天 11:12
重庆大学 C++
既然这么缺人,为什么挂我呢
希望被offer砸中...:其实不缺人
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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