当前位置:网站首页>Leetcode-22: bracket generation
Leetcode-22: bracket generation
2022-07-05 06:09:00 【Chrysanthemum headed bat】
leetcode-22: Bracket generation
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 .
Example 1:
Input :n = 3
Output :["((()))","(()())","(())()","()(())","()()()"]
Example 2:
Input :n = 1
Output :["()"]
Problem solving
If direct violence is traced , Give Way ’(' and ‘)' Free combination , Then judge the validity of brackets , To do so , The complexity will be relatively high .
Therefore, we can directly pass some judgment conditions , Make the generated parentheses valid .
Method 1 : to flash back
adopt open ,close To count Number of opening and closing brackets , Guarantee close<open Will add closed parentheses , To ensure the validity of the generated parentheses .
class Solution {
public:
string path;
vector<string> res;
void dfs(int open,int close,int n){
if(path.size()==n*2){
res.push_back(path);
return;
}
if(open<n){
path.push_back('(');
dfs(open+1,close,n);
path.pop_back();
}
if(close<open){
path.push_back(')');
dfs(open,close+1,n);
path.pop_back();
}
}
vector<string> generateParenthesis(int n) {
dfs(0,0,n);
return res;
}
};
边栏推荐
- SPI 详解
- Fried chicken nuggets and fifa22
- EOJ 2021.10 E. XOR tree
- LeetCode 0107.二叉树的层序遍历II - 另一种方法
- Dichotomy, discretization, etc
- Flutter Web 硬件键盘监听
- CF1634 F. Fibonacci Additions
- [cloud native] record of feign custom configuration of microservices
- Individual game 12
- Multi screen computer screenshots will cut off multiple screens, not only the current screen
猜你喜欢
![[article de jailhouse] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[article de jailhouse] jailhouse hypervisor

Wazuh開源主機安全解决方案的簡介與使用體驗

On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech

【实战技能】如何做好技术培训?

shared_ Repeated release heap object of PTR hidden danger

网络工程师考核的一些常见的问题:WLAN、BGP、交换机

leetcode-6110:网格图中递增路径的数目

SQLMAP使用教程(一)

6. Logistic model

EOJ 2021.10 E. XOR tree
随机推荐
[rust notes] 16 input and output (Part 1)
7. Processing the input of multidimensional features
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
leetcode-22:括号生成
Transform optimization problems into decision-making problems
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
1040 Longest Symmetric String
传统数据库逐渐“难适应”,云原生数据库脱颖而出
1039 Course List for Student
wordpress切换页面,域名变回了IP地址
Wazuh开源主机安全解决方案的简介与使用体验
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
927. Trisection simulation
数据可视化图表总结(二)
[rust notes] 14 set (Part 1)
[rust notes] 17 concurrent (Part 2)
[rust notes] 13 iterator (Part 2)
2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
【Rust 笔记】15-字符串与文本(上)
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!