博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
22. Generate Parentheses
阅读量:7175 次
发布时间:2019-06-29

本文共 1366 字,大约阅读时间需要 4 分钟。

22. Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[  "((()))",  "(()())",  "(())()",  "()(())",  "()()()"]
1 /** 2  * @param {number} n 3  * @return {string[]} 4  */ 5 var generateParenthesis = function(n) { 6      7      var res = []; 8      9     10     // n == 011      if(n == 0){12          return res;13      }14     15     //n == 1;16      if(n == 1){17          18          res.push("()");19          return res;20      }21     22     23     24     25     //首先你会发现开头一定是"(",结尾一定是")";26     27     //还有一个明显的规律是 一定先有 "(" 再有")",也就是说,在没有到最后一个")"之前,"("的数目比右括号多28     29   30 31             32     function innergenerateParenthesis(str,l,r,n){33       34          if(str.length == n*2){35              36              res.push(str);37              return;38          }39         40          if(l < n){41              42          43              innergenerateParenthesis(str+"(",l+1,r,n);44              45          }46         47          if(r < l){48              49              50              innergenerateParenthesis(str+")",l,r+1,n);51              52          } 53         54     }55     56     57     innergenerateParenthesis("",0,0,n);58     59     60     return res;61      62 };

 

转载于:https://www.cnblogs.com/huenchao/p/7667755.html

你可能感兴趣的文章
如何从硬盘安装WIN7原版镜像?
查看>>
ceph学习笔记之六 数据读写过程
查看>>
为UC做准备:准备Exchange 2010的先决条件
查看>>
Eclipse快捷键大全(转载)
查看>>
weblogic启动脚本
查看>>
cPanel推出测试免费试用
查看>>
Wireshark的https代理抓包(whistle中间人代理)
查看>>
redhat下安装Wineqq2012
查看>>
linux下配置pptp 客户端
查看>>
java发送短信至手机
查看>>
linux系统开机流程
查看>>
rsyslog+loganalyzer+evtsys搭建集中式监控系统
查看>>
JDBC简单介绍一
查看>>
关于对象的自我赋值行为
查看>>
6.5版 Samba服务器搭建
查看>>
分享27个谷歌(Google)镜像
查看>>
读<王垠:一种新的操作系统设计>
查看>>
Eclipse maven构建springmvc项目
查看>>
js设置组合快捷键/tabindex功能的方法
查看>>
自动检测域内电脑的USB端口是否开启的脚本
查看>>