当前位置:网站首页>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;
}
};
边栏推荐
- Data visualization chart summary (II)
- Full Permutation Code (recursive writing)
- [jailhouse article] look mum, no VM exits
- 对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
- Bit mask of bit operation
- Doing SQL performance optimization is really eye-catching
- Common optimization methods
- Typical use cases for knapsacks, queues, and stacks
猜你喜欢
Redis publish subscribe command line implementation
SPI details
[cloud native] record of feign custom configuration of microservices
传统数据库逐渐“难适应”,云原生数据库脱颖而出
Wazuh開源主機安全解决方案的簡介與使用體驗
CF1634E Fair Share
Introduction to LVS [unfinished (semi-finished products)]
LaMDA 不可能觉醒吗?
Is it impossible for lamda to wake up?
Fried chicken nuggets and fifa22
随机推荐
Convolution neural network -- convolution layer
【Rust 笔记】17-并发(下)
Implement a fixed capacity stack
【Jailhouse 文章】Jailhouse Hypervisor
R语言【数据集的导入导出】
Appium自动化测试基础 — Appium测试环境搭建总结
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
CPU内核和逻辑处理器的区别
Sqlmap tutorial (II) practical skills I
Open source storage is so popular, why do we insist on self-development?
QQ电脑版取消转义符输入表情
[rust notes] 16 input and output (Part 2)
Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
Overview of variable resistors - structure, operation and different applications
【Rust 笔记】17-并发(上)
从Dijkstra的图灵奖演讲论科技创业者特点
Introduction et expérience de wazuh open source host Security Solution
Dynamic planning solution ideas and summary (30000 words)
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
A reason that is easy to be ignored when the printer is offline