题目

给定括号对数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的更多相关文章

  1. Leetcode 22.生成括号对数

    生成括号对数 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "( ...

  2. LeetCode 22. 括号生成(Generate Parentheses)

    题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "(() ...

  3. LeetCode 笔记系列五 Generate Parentheses

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

  4. leetcode第21题--Generate Parentheses

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

  5. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  6. Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)

    Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...

  7. [LeetCode] 22. Generate Parentheses 生成括号

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

  8. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  9. [LeetCode] Generate Parentheses 生成括号

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

  10. 22.Generate Parentheses[M]括号生成

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

随机推荐

  1. Office常见问题及解决方法

    1.OneNote粘贴的图片比较模糊 问题原因:Microsoft为了使OneNote排版美观,在粘贴一些尺寸较大的图片时,会将图片进行缩放. 解决方法:右键->还原为原始尺寸

  2. prometheus-alertmanager 告警规则

    # 告警规则参考 https://awesome-prometheus-alerts.grep.to/rules

  3. rsyslog由于RateLimit丢失日志的处理

    问题 问题的发现:在我们的docker应用中,配置了rsyslog作为日志输出:当应用产生的日志在某段时间内量比较大时,就发现会丢日志. 原因 问题的原因:日志的输出路径,应用程序把日志输出到syst ...

  4. IDEA通过Spring Initalizr新建SSM (2)

    之前的方式是通过官网初始化demo(URL:https://start.spring.io/)现在记录一下通过IDEA自带的初始化器新建SSM框架 1.打开IDEA,点击新建,出现如下图菜单,点击Sp ...

  5. C#动态创建对象和其属性

    dynamic contact = new ExpandoObject(); contact.Name = "Patrick Hines"; contact.Phone = &qu ...

  6. Kettle初使用

    Kettle的使用还是比较简单,但使用过程中会遇到一些问题: 1.mysql8以上版本连接数据库的时候汇报如下错误: 解决方法:参考链接:https://www.cnblogs.com/wuzaipe ...

  7. 2022-04-24内部群每日三题-清辉PMP

    1.在估算项目成本时,项目经理与一位主题专家(SME)合作,该专家曾有低估交付项目需求所需工作的历时.然而,在所有其他领域,该主题专家是一位很好的贡献者,备受尊重,并且经常有相关方需要他.若要主动减轻 ...

  8. Oracle深入数据库体系结构_第三章文件

    构成数据库和实例的8种主要文件类型: 参数文件:(init file) 可以通过视图V$PARAMETER(show parameter)查看 跟踪文件 警告文件 数据文件 临时文件 控制文件 重做日 ...

  9. JavaScript Math(算数) 对象

    JavaScript Math(算数) 对象 Math(算数)对象的作用是:执行常见的算数任务. 在线实例 round()如何使用 round(). random()如何使用 random() 来返回 ...

  10. HCIA-ICT实战基础09-远程接入安全管理

    HCIA-ICT实战基础-远程接入安全管理 目录 AAA概述 AAA配置实现 telnet原理与配置 Stelnet(华为ssh的另一种称呼)配置 1 AAA概述 1.1 基本概念 AAA是Authe ...