[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 ...
随机推荐
- xd p4 WEB源码拓展
WEB 源码在安全测试中是非常重要的信息来源,可以用来代码审计漏洞也可以用来做信息突破口,其中 WEB 源码有很多技术需要简明分析. 知识点 关于 WEB 源码目录结构 后台目录.模板目录.数据库目录 ...
- python os.path模块函数功能
1.os.path.abspath(path) 获取绝对路径,实际上等于os.getcwd()+path 2.os.path.basename(path)取path最后的文件或文件名.如果path以/ ...
- PostgreSQL 解析json字段
一.解析json数组 select json_array_elements(lv_num_json)->'l1' lv,json_array_elements(lv_num_json)-> ...
- Java课堂学习总结
对于Java字段的初始化来说,共有以下几种方法: 1.类的构造函数(构造方法):当创建一个对象时,其构造函数就会自动调用.构造方法必须与类名相同,无返回值.如果程序员没有定义一个构造函数时,系统会自动 ...
- pushd 和 popd
可以把你当前的文件路径 放进一个栈里,后边拿出来 很方面的skim方法
- SAP GGB0 校验
需求,针对财务凭证分配号的要求 在满足条件下进行必填校验 在需要的位置 创建确认 创建步骤,一般通过点击就可以形成需要的前提逻辑,也可以通过 设置->专门方式 来进行自定义编写. 如果前提条件是 ...
- vue 3 打印 print-js
1.安装 npm install print-js --save 2.引用 import print from 'print-js' 3.编写打印函数 const enterDialog = asyn ...
- 几个一看就会的实用JavaScript优雅小技巧
️ 前言 这次我就给大家分享一些一看就会的实用JavaScript优雅小技巧. 「难度:」 「推荐阅读时长:5min」 正片 减少if...else面条代码 一旦当我们写到超过两个if...else的 ...
- React 事件绑定this指向
1. 推荐:使用class的实例方法 class Hello extends React.Component { handleClick = () => { this.setState({ .. ...
- JS篇(008)-require 与 import 的区别
答案:两者的加载方式不同.规范不同 第一.两者的加载方式不同,require 是在运行时加载,而 import 是在编译时加载 require('./a')(); // a 模块是一个函数,立即执行 ...