当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
实时时钟 (RTC)
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
AtCoder Grand Contest 013 E - Placing Squares
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
RGB LED infinite mirror controlled by Arduino
Appium基础 — 使用Appium的第一个Demo
API related to TCP connection
In this indifferent world, light crying
从Dijkstra的图灵奖演讲论科技创业者特点
随机推荐
Brief introduction to tcp/ip protocol stack
Introduction and experience of wazuh open source host security solution
1.14 - 流水线
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
Collection: programming related websites and books
[practical skills] how to do a good job in technical training?
redis发布订阅命令行实现
1039 Course List for Student
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
LeetCode 1200.最小绝对差
Groupbykey() and reducebykey() and combinebykey() in spark
How to adjust bugs in general projects ----- take you through the whole process by hand
The sum of the unique elements of the daily question
Transform optimization problems into decision-making problems
SPI 详解
Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
1040 Longest Symmetric String
[article de jailhouse] jailhouse hypervisor
Arduino 控制的 RGB LED 无限镜
LVS简介【暂未完成(半成品)】