题目

给定括号对数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. 蓝桥杯训练赛二-1169 问题 D: 绝对值排序

    题目描述 输入n(n<=100)个整数,按照绝对值从大到小排序后输出.题目保证对于每一个测试实例,所有的数的绝对值都不相等. 输入 输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整 ...

  2. 小程序modal弹窗

    [注意]css放的位置可能影响效果 参考a-level competitionFilterCover 1.容器:position: absolute; top: 100vh; animation: m ...

  3. Web开发 学习 调试 调优

    目录 快捷操作 调试方法 基本调试方法 修改参数和请求重发 Chrome抓包分析 性能优化 安全 cURL请求 参考 参考:MDN 调试HTML 参考:什么是浏览器开发者工具? 参考:检查和编辑页面与 ...

  4. HDLbits——Shift18

    // Build a 64-bit arithmetic shift register, // with synchronous load. The shifter can shift both le ...

  5. 解决GitHub网页githubusercontent地址无法访问问题

    问题 解决GitHub网页githubusercontent地址无法访问问题 解决方法: 参考链接:https://zhuanlan.zhihu.com/p/107691233 注意 安装有火绒的,可 ...

  6. VUE2.0 脚手架搭建项目,如何配置本地IP地址访问项目,详解

    1.首先找到config文件夹目录下的 index.js文件 // Various Dev Server settings //host: 'localhost' //将localhost进行替换成 ...

  7. QPushButton CSS样式

    QPushButton:!hover { color:white; font: 20pt "楷体"; border-radius:10px; border:1px solid rg ...

  8. etcd使用Cfssl生成自签证书(pem)

    CFSSL是CloudFlare开源的一款PKI/TLS工具,CFSSL包含一个命令行工具和一个用于签名,验证并且捆绑TLS证书的HTTP API服务,环境构建方面需要 Go 1.12+. 需要两套证 ...

  9. java 泛型使用

    泛型类 // 简单泛型 class Point<T>{ // 此处可以随便写标识符号,T是type的简称 private T var ; public T getVar(){ return ...

  10. OWASP ZAP基本使用教程(Kali版)

    简介OWASP ZAP是一款非常好用的测试工具,也是Kali里自带的工具,一键就可以扫描多种不同类型的漏洞,最好用的一点就是他可以自动爬取子域名.非常的快捷方便下面我就给大家带来OWASP ZAP的基 ...