当前位置:网站首页>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
边栏推荐
- Realizing data value through streaming data integration (5) - stream processing
- 2022 mobile crane driver test question bank simulation test platform operation
- DBA common SQL statements (1) - overview information
- 一文看懂 LSTM(Long Short-Term Memory)
- Realizing data value through streaming data integration (4) - streaming data pipeline
- Download and installation steps of xshell + xftp
- /etc/shadow可以破解吗?
- Ansible cloud computing automation command line compact version
- Jerry's factors that usually affect CPU performance test results are: [article]
- 【无标题】
猜你喜欢
Sim Api User Guide(6)
MapReduce core and foundation demo
Juc并发编程09——Condition实现源码分析
Redis design and Implementation
How can swagger2 custom parameter annotations not be displayed
[untitled]
Sim Api User Guide(4)
SSH uses private key to connect to server without key
Sim Api User Guide(5)
Swagger2 自定义参数注解如何不显示
随机推荐
Jerry's more accurate determination of abnormal address [chapter]
19. Delete the penultimate node of the linked list (linked list)
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
Sim Api User Guide(4)
Leetcode22:括号生成
SSH uses private key to connect to server without key
JVM——》常用命令
142、环形链表||
LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Ansible cloud computing automation command line compact version
Juc并发编程06——深入剖析队列同步器AQS源码
ansible 云计算 自动化 命令行精简版
定义链表(链表)
lnmp的配置
1. Sum of two numbers (hash table)
JVM——》常用参数
[untitled]
景联文科技—专业数据标注公司和智能数据标注平台
Realize data value through streaming data integration (2)