[LeetCode]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.左括号的个数必须小于给出的个数n
基于上面的条件,我们设计回溯的时候需要先放左括号,在有左括号的基础上才能有右括号,所以我们设置2个left和right来计算左右括号的个数
left<n时可以放置左括号,然后递归进去处理,当left=n时放右括号,right<left,这个条件很重要,如果没有的话可能导致在第1个位置left=0时放置右括号,这是不合法序列
最后list.length==2*n时返回
class Solution {
public List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList();
backtrack(ans, "", 0, 0, n);
return ans;
}
public void backtrack(List<String> ans, String cur, int open, int close, int max){
if (cur.length() == max * 2) {
ans.add(cur);
return;
}
if (open < max)
backtrack(ans, cur+"(", open+1, close, max);
if (close < open)
backtrack(ans, cur+")", open, close+1, max);
}
}
回溯就类似往栈里放东西,先进后出,所以所有可能性的个数就是一个卡特兰数,时间复杂度也就是这个卡特兰数
[LeetCode]22. Generate Parentheses括号生成的更多相关文章
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [LeetCode] 22. Generate Parentheses ☆☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
随机推荐
- Eclipse 使用TFS
Install Soft , –> add http://dl.microsoft.com/eclipse/tfs form:http://msdn.microsoft.com/en-us/ ...
- nRF51822外设应用[2]:GPIOTE的应用-按键检测
版权声明:本文为博主原创文章,转载请注明作者和出处. 作者:强光手电[艾克姆科技-无线事业部] 1. nRF51822寄存器类型 nRF51822的寄存器和一般的单片机有所差别,nRF51822 ...
- jQuery 遍历 - closest() 方法
jQuery 遍历参考手册 实例 本例演示如何通过 closest() 完成事件委托.当被最接近的列表元素或其子后代元素被点击时,会切换黄色背景: $( document ).bind("c ...
- java 的在线下载文件 .pdf
java 的在线下载文件 .pdf 1.下载资源的本地位置 2.设置响应头 3.下载代码 1 PeriodicalResource periodicalResource = periodicalR ...
- grafana使用小节
安装准备 安装grafana 安装mysql grafana操作步骤 新建数据源,支持mysql 数据库连接失败处理: https://www.jianshu.com/p/684bc3a77ac9 新 ...
- 不带 www 跳转 到 带 www 网站..
IIS: <rule name="已导入的规则 1-1" stopProcessing="true"> <match url="^( ...
- 【算法笔记】B1041 考试座位号
1041 考试座位号 (15 分) 每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生 ...
- Apache Shiro(三)-登录认证和权限管理MD5加密
md5 加密 在前面的例子里,用户密码是明文的,这样是有巨大风险的,一旦泄露,就不好了.所以,通常都会采用非对称加密,什么是非对称呢?就是不可逆的,而 md5 就是这样一个算法.如代码所示 123 用 ...
- webpacke
webpacke 安装 首先要安装 Node.js, Node.js 自带了软件包管理器 npm 用npm 安装webpack $ npm install webpack -g 此时 Webpac ...
- 基于Map的用户注册、登录、抽奖系统
期望功能 方法设计 1.类的设计: 定义两个参与者的属性: private String username; private String userpassword; 2.注册方法: public v ...