[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 ...
随机推荐
- 全链路压测SOP
压测模型构建:人工 线上(大促)流量数据 (数据脱敏) 日常流量数据 业务方新的特性产生的变更数据 友商做过的事情 压测模型构建:自动 流程包括:录制-清洗-回放 (目前能做好的公司非常少) 压测标准 ...
- k8s入门_RC、RS、Deployment
RC 什么是RC: Replication Controller(副本控制器),RC能够保证pod在任意时间运行的副本数量,能够保证pod总是可用的. RC控制的pod的多个副本,每个副本都有独立的i ...
- Vue 的生命周期 详细解析(使用场景等)
Vue生命周期图: 一.生命周期图的解读 new Vue():首先需要创建一个 Vue的实例对象 Init Events & Lifecycle :初始化:生命周期.事件(如:v-once), ...
- stream 链式结构 求和
Double aDouble = Optional.ofNullable(wayfairMonthBill.getPaymentAmountDetailJson()) .filter(StringUt ...
- 解决uniapp 使用自带 switch 双向绑定视图不更新的问题
使用 this.$set( a, b, c) a:需要更新视图属性对象 b:具体的属性值(就是你要更新视图的属性值) c:传递的参数 this.$set(this.gwjSelet,this.gwj ...
- 洛谷 P4454 [CQOI2018]破解D-H协议
题目 https://www.luogu.com.cn/problem/P4454 杂题乱做ing... 思路 首先我们把式子列一下: \(g^a\equiv A(mod P)\) \(g^b\equ ...
- 光纤加速卡第410篇:基于XCVU9P+ C6678的40G光纤的加速卡 光纤的加速卡 无线通信
光纤加速卡第410篇:基于XCVU9P+ C6678的40G光纤的加速卡 光纤的加速卡 无线通信 基于XCVU9P+ C6678的40G光纤的加速卡 一.板卡概述 二.技术指标 • 板卡为自定义 ...
- JS学习-async/await
async/await 它保证函数的返回值为 promise. 用更少的.then()块来封装代码,同时它看起来很像同步代码 注意:可能会因为大量await的promises相继发生而变慢. asyn ...
- k8s master高可用
每台master都要部署haproxy,keepalived keepalived 配置文件:! Configuration File for keepalivedglobal_defs { rout ...
- 【Docker】基本使用
服务 启动docker systemctl start docker 重启docker systemctl restart docker 停止docker systemctl stop docker. ...