题解 | #括号匹配方案#

括号匹配方案

http://www.nowcoder.com/practice/380380e6c6b444888ae145593ccbbbca

//1.遍历字符串如果是左括号就+1 如果是右括号就-1
//2.在遇到左括号时就执行1*2*3...。
const readline = require("readline")
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})
rl.on("line", function (n) {
  let index = 1
  let res = 1
  n.split("").forEach((item) => {
    if (item === "(") {
      res *= index
      index++
    } else {
      index--
    }
  })
  console.log(res)
})

function gcd(x, y) {
  return x % y === 0 ? y : gcd(y, x % y)
}

rl.on("close", function () {
  process.exit()
})
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务