题目

给定括号对数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. CUDA的新功能

    CUDA 9: 配合Volta架构推出: 1. 新的多线程编程范式.Cooperative Groups 2. 优化算法库 CUDA10: 配合Turing架构推出. 1. 新增了对TensorCor ...

  2. Jmeter接口测试+Jmeter性能测试项目实战+Jmeter性能监控分析调优01

    Jmeter P1 性能测试关键指标 1,这些指标大家看到后有什么感觉呢?对于功能测试,一般结果就两种,成功的或者是不通过的,有问题的和没问题的,有毛病的和没毛病的,比如打开网页,能打开就打开,打不开 ...

  3. javascript【应用】debounce和throttle

    debounce防抖 在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时:典型的案例就是输入搜索:输入结束后n秒才进行搜索请求,n秒内又输入的内容,就重新计时. <div id=& ...

  4. 通过EXCEL/WPS文件,拼接SQL,刷数据库数据

    WPS如何把日期变成文本格式? [快捷选择同一列多条记录]同一列,鼠标左键标记A,SHIFT+鼠标左键标记B ,等于选择A-B的之间的数据 [向下填充]在第一行输入数据,选择同一列A-B的之间的数据, ...

  5. 摹客RP,编辑界面缩放比例支持手动输入!

    Hello,小伙伴们,又到了摹客的新功能播报时间. 本月更新,摹客RP编辑界面缩放比例支持手动输入,并对部分组件的默认样式及属性进行了优化:摹客DT率先上线了3款黄金比例图层,辅助设计师更高效绘图:针 ...

  6. Centos安装后出现please make your choice from '1' to eter the license information spoke | 'q' to quit |'c' to continue |'r' to refresh

    这是要求用户阅读或者接收协议: 解决方法:输入"1",按Enter键   阅读许可协议,输入"2",按Enter键  接受许可协议,输入"q" ...

  7. 像MIUI一样做Zabbix二次开发(2)——Zabbix与商业及开源产品的比较分析

    Zabbix的特点 Zabbix是一个基于WEB界面的提供分布式系统监控的开源解决方案,通过浏览器监视,做到告警分级处理.网络访问安全可控,该开源平台在全球有广泛的用户基础,他的特点: 开放性:源代码 ...

  8. Pytorch Dropout函数

    Dropout layers class torch.nn.Dropout(p=0.5, inplace=False) 随机将输入张量中部分元素设置为0.对于每次前向调用,被置0的元素都是随机的. 参 ...

  9. PHP基础教程(二)

    本部分列出了在 PHP 中使用的各种运算符:算数运算符 运算符 说明 例子 结果 + Addition x=2x+2 4 - Subtraction x=25-x 3 * Multiplication ...

  10. FSCapture怎么取色

    启动软件后 点击setting->Screen color Picker 此时箭头会变为一个吸管形状, 选择要吸取的颜色,点击鼠标左键 出现如图所示 完了复制这个十六进制颜色值