[Leetcode 22]生成括号generate parentheses
题目
给定括号对数n,生成所有可能的标准括号结果*
*指不能)(
https://leetcode.com/problems/generate-parentheses/
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Example 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:
Input: n = 1
Output: ["()"]
思路
dfs,回溯
生成括号的情况:
当前左括号数<总括号对数+((保证生成n对括号)
当前右括号数<左括号数,+)(保证先左括号再右括号)
当前字符串len=括号对数*2,将结果保存到全局list中,return
代码
class Solution {
List<String> ans=new ArrayList();
public List<String> generateParenthesis(int n) {
fun("",0,0,n);
//用stringbuilder速度会更快
return ans;
}
public void fun(String cur,int left,int right,int max){
if(cur.length()==max*2){
ans.add(cur);//一对括号 长度*2
return;
}
if(left<max)
fun(cur+"(",left+1,right,max);//左括号开头,他和总对数比
if(right<left)
fun(cur+")",left,right+1,max);//右括号必在左括号后(well-formed要求)
}
}
[Leetcode 22]生成括号generate parentheses的更多相关文章
- Leetcode 22.生成括号对数
生成括号对数 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "( ...
- LeetCode 22. 括号生成(Generate Parentheses)
题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "(() ...
- LeetCode 笔记系列五 Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- leetcode第21题--Generate Parentheses
problem: Given n pairs of parentheses, write a function to generate all combinations of well-formed ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)
Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22.Generate Parentheses[M]括号生成
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
随机推荐
- ASPICE的实践
ASPICE这种规范文件如果严格遵守,那么投入产出比是比较小的. 但是其中的思想是很好的,比如对需求的管理.讲究双向可追溯. 但是也是有很多需要慎重的,比如对component和unit的区分太过明显 ...
- UltiSnips安装及设置
2022-10-05 10:56:50 星期三 安装了UltiSnips插件,然后开始学习 第一个命令 UltiSnipsEdit 不好使,创建了~/.vim/UltiSnips 还是 can not ...
- FB50 过帐码 没有定义
FB50 提示"过帐码 没有定义" --ECC6.0&Logon730 转至事务码:OBX1 或找到->定义总账科目过账的过账吗 定义借记 40 贷方 50,保存即 ...
- ORACLE之自治事务
在创建存储过程时,可以用关键字: PRAGMA AUTONOMOUS_TRANSACTION 创建自治事务. 常用场景: 触发器抛错导致事务回滚,日志的记录需要用自治事务进行记录 代码抛错,导致数据库 ...
- 自定义注解+反射提取对象到map中
一.问题:有时候我们与第三方接口对接传参时,需要将对象里的字段和值以map形式传给别人,此时可以借助其他的工具类,但是我个人用起来不太灵活,还会把多余的字段传给别人,因此我们自己动手搞一套 二.思路: ...
- bladex 通用日志不入库,日志无法使用
公司最近要记录操作日志,然后用的是bladex脚手架.想起来刚好有自带的日志管理功能,然后就很happy的将日志功能授权给了管理员. 也就是那几个通用日志.接口日志.错误日志. 结果人傻了,根本不会记 ...
- gin web 2
routers/router.go package routers import ( "github.com/gin-gonic/gin" "gin-blog/pkg/s ...
- 像这个TSC打印机售后维修服务平台网站,是如何做到复制不到网站里面的内容的?
这个条码之家是TSC打印机售后维修服务平台网站 网址是这个,http://www.tiaomazhijia.com 打开网站,复制不了里面的内容,右健也另存不了图片等,是如何做到的. tsc显示错误亮 ...
- C#textbox更改字体颜色只读后不起作用的解决办法
textbox的属性ReadOnly设置为true只读后,只更改字体颜色并不起作用. 解决办法是,连同背景色一起设置即可. textBox1.BackColor =textBox1.BackColor ...
- Pycharm实现sqlite数据库可视化