[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 ...
随机推荐
- swing01
基于Eclipse和Mysql 快捷键: 类的备注:/** +enter main方法快捷方式:main alt+/ 自动提示:alt+/ 提示报错:ctrl+1 输出语句快捷方式:syso alt+ ...
- Linux安装证书
Linux安装 vCenter root CA: 1.访问vCenter管理页面,下载"下载受信任的根 CA 证书" 2.压缩文件内带有数字作为扩展名(.0..1 等)的文件是根证 ...
- BeanUtils.copyProperties null覆盖问题
直接用一下工具类 public class CopyUtils { public static String[] getNullPropertyNames (Object source) { fina ...
- FB50 过帐码 没有定义
FB50 提示"过帐码 没有定义" --ECC6.0&Logon730 转至事务码:OBX1 或找到->定义总账科目过账的过账吗 定义借记 40 贷方 50,保存即 ...
- gateway 网关接口防篡改验签
gateway 网关接口防篡改验签 背景:为了尽可能降低接口在传输过程中,被抓包然后篡改接口内的参数的可能,我们可以考虑对接口的所有入参做签名验证,后端在网关依照相同的算法生成签名做匹配,不能匹配的返 ...
- protobuf如何还原proto源文件及描述字符串中左括弧的意义
语法分析 和通常的有语法的结构一样,proto的编译也经过词法(tokenize)和语法(parse)两个阶段,相关代码分别在tokenizer.cc和parser.cc两个文件中. /// @fil ...
- ASPNET Core ActionFilterAttribute中断后续请求
转载自:https://www.cnblogs.com/luconsole/p/4346669.html SPNET MVC如何正确的中断请求? 感觉是这样? 在aspnet开发过程中如果想要中断当前 ...
- centos7 部署 DNS 主从
centos7 部署 DNS 主从 环境 名称 ip地址 cpu 内存 yz-dns-master 10.148.100.81 4c 8G yz-dns-slave 10.148.100.82 4c ...
- Linux一键单机部署和集群部署
整个部署脚本只用执行sh即可,有需要可以联系我. 一.部署类型 可参考:常见的部署类型(停机部署.蓝绿部署.滚动部署.灰度部署.AB测试等) 二.一键单机部署Docker服务 三.一键单机部署原生服务 ...
- 通过python程序让MySQL利用binlog恢复误操作数据
MySQL利用binlog恢复误操作数据 在人工手动进行一些数据库写操作的时候(比方说数据订正),尤其是一些不可控的批量更新或删除,通常都建议备份后操作.不过不怕万一,就怕一万,有备无患总是好的.在线 ...