题解 | #括号生成#

括号生成

http://www.nowcoder.com/practice/c9addb265cdf4cdd92c092c655d164ca

/**
  * 
  * @param n int整型 
  * @return string字符串一维数组
  */
function generateParenthesis( n ) {
    // write code here
    let result = []
  //let temp = n.split('');
  dfs(n, n, '', result);  // 一开始左右括号的数量都为n
  return result;
   function dfs (left,right,str,result){
    if(left > right)return
    if(left === 0 && right === 0 ){
      result.push(str)
    }
    if(left > 0){
      dfs(left-1,right,str+'(',result)
    }
    if(right > 0){
      dfs(left,right-1,str+')',result)
    }
  }
}
module.exports = {
    generateParenthesis : generateParenthesis
};
全部评论

相关推荐

11-21 14:37
江苏大学 Java
点赞 评论 收藏
分享
11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
包行:平时怎么刷算法题的哇,字节的手撕听说都很难
字节跳动工作体验
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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