当前位置:网站首页>Leetcode22: bracket generation
Leetcode22: bracket generation
2022-04-23 10:31:00 【VipPeterGee】
subject
- Numbers n Represents the logarithm of the generated bracket , Please design a function , Used to be able to generate all possible and Effective Bracket combination
solution
private static List<String> generate(int n) {
List<String> dataList=new ArrayList<>();
if (n==0)return dataList;
dfs("",n,n,dataList);
return dataList;
}
- DFS Do subtraction
private static void dfs(String current, int left, int right, List<String> dataList) {
// The end of recursion is added to list in
if (left==0&&right==0){
dataList.add(current);
return;
}
if (left>right){
return;
}
if (left>0){
dfs(current+"(",left-1,right,dataList);
}
if (right>0){
dfs(current+")",left,right-1,dataList);
}
}
- DFS Addition method
private static void dfsIncrease(String current, int left, int right,int length, List<String> dataList) {
// The end of recursion is added to list in
if (left==length&&right==length){
dataList.add(current);
return;
}
if (left<right){
return;
}
if (left<length){
dfsIncrease(current+"(",left+1,right,length,dataList);
}
if (right<length){
dfsIncrease(current+")",left,right+1,length,dataList);
}
}
- Reference resources : https://leetcode-cn.com/problems/generate-parentheses/solution/hui-su-suan-fa-by-liweiwei1419/
版权声明
本文为[VipPeterGee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231006408994.html
边栏推荐
- 得到知识服务app原型设计比较与实践
- Can Jerry's AES 256bit [chapter]
- Chapter 1 Oracle database in memory related concepts (im-1.1)
- 微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
- Realizing data value through streaming data integration (4) - streaming data pipeline
- Using multithreading to output abc10 times in sequence
- JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
- CSP certification 202203-2 travel plan (multiple solutions)
- 202. Happy number
- Realize data value through streaming data integration (2)
猜你喜欢

Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022

JUC concurrent programming 09 -- source code analysis of condition implementation

JVM——》常用参数

IDEA——》每次启动都会Indexing或 scanning files to index

JUC concurrent programming 07 -- is fair lock really fair (source code analysis)

Comparison and practice of prototype design of knowledge service app

Question bank and answers of Shanghai safety officer C certificate examination in 2022

精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]
![[untitled]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[untitled]
随机推荐
DBA common SQL statements (5) - latch related
Linked list intersection (linked list)
景联文科技—专业数据标注公司和智能数据标注平台
Realize data value through streaming data integration (2)
707、设计链表(链表)
202. Happy number
Ansible playbook syntax and format automate cloud computing
242. Valid Letter ectopic words (hash table)
Using idea to develop Spark Program
微信小程序中app.js文件、组件、api
DBA common SQL statements (2) - SGA and PGA
Leetcode22:括号生成
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
203. Remove linked list elements (linked list)
203、移出链表元素(链表)
What are the system events of Jerry's [chapter]
Yarn core parameter configuration
Juc并发编程07——公平锁真的公平吗(源码剖析)
Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
Charles 功能介绍和使用教程