[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 ...
随机推荐
- lxml库和BeautifulSoup库常用点小结
算是本人的学习笔记吧,仅供个人学习使用. 以下内容摘自<Python3网络爬虫开发实战--崔庆才著> 1.lxml库 XPath 常用规则: 表达式 描述 nodename 选取此节点的所 ...
- 关于sql json数据的处理
$resultProductPrice = DB::update("update lev_product_price set detail=json_set(detail,'$.颜色','红 ...
- pytorch 独热编码报错的解决办法:one_hot is only applicable to index tensor
首先,报错原因,我认为是数据类型错误, 在文档中表示,第一个tensor参数的数据类型为LongTensor,也就是torch.int64类型的,如果你有报这个错:"one_hot is o ...
- Spring Security 自定义认证逻辑
Spring Security 自定义认证逻辑 这篇文章的内容基于对Spring Security 认证流程的理解,如果你不了解,可以读一下这篇文章:Spring Security 认证流程 . 分析 ...
- unity Dotween Path 设置路径平滑加旋转平滑
Waiter.transform.DOPath(list.ToArray(), 3f * (index / 2.0f + 1.0f), PathType.CatmullRom).SetLookAt(0 ...
- 网络图片转InputStream,网络图片转MultipartFile,InputStream转MultipartFile
头疼,不废话直接上代码 <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> ...
- 用Python中的hashlib实现md5和sha加密
文章目录 一.用 pandas 读取 Excel 数据 二.加密库 hashlib 三. pandas中的 map() 方法 四.数据加密工具 本文分享知识: pandas读取Excel数据 read ...
- 基于Python接口自动化测试持续集成----在jenkins创建任务->检出git的代码-->生成报告-->发送邮件
步骤一:先在jenkins创建一个自由风格的任务 步骤二:然后在源码管理选择git检出代码的方式,需要提供代码所在git的路径和登录git的账号和密码 步骤三:返回到任务配置的界面,先在构建后操作增加 ...
- pycharm cv2 的方法不能智能提示
按住ctrl,光标放在cv2上,就能跳转到cv2的__init__.py文件 全选,按Ctrl+/注释掉所有语句,然后将如下语句添加到__init__.py中 import sys import os ...
- 常用的CSS效果(1)
单行省略 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 多行省略 display:-webkit-box; overf ...