当前位置:网站首页>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;
}
};
边栏推荐
- QQ computer version cancels escape character input expression
- QQ电脑版取消转义符输入表情
- Arduino 控制的 RGB LED 无限镜
- SQLMAP使用教程(二)实战技巧一
- Open source storage is so popular, why do we insist on self-development?
- 927. Trisection simulation
- Shutter web hardware keyboard monitoring
- leetcode-556:下一个更大元素 III
- Flutter Web 硬件键盘监听
- SPI details
猜你喜欢

Solution to game 10 of the personal field

开源存储这么香,为何我们还要坚持自研?
![[article de jailhouse] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[article de jailhouse] jailhouse hypervisor

F - Two Exam(AtCoder Beginner Contest 238)

Implement a fixed capacity stack

Liunx starts redis
![[jailhouse article] look mum, no VM exits](/img/fe/87e0851d243f14dff96ef1bc350e50.png)
[jailhouse article] look mum, no VM exits

LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树

Full Permutation Code (recursive writing)

SPI 详解
随机推荐
927. Trisection simulation
1039 Course List for Student
Data visualization chart summary (II)
从Dijkstra的图灵奖演讲论科技创业者特点
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
MIT-6874-Deep Learning in the Life Sciences Week 7
Multi screen computer screenshots will cut off multiple screens, not only the current screen
【Rust 笔记】16-输入与输出(上)
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
1041 Be Unique
Redis publish subscribe command line implementation
PC register
CF1634E Fair Share
leetcode-9:回文数
Full Permutation Code (recursive writing)
Doing SQL performance optimization is really eye-catching
1.15 - 输入输出系统
Dichotomy, discretization, etc
Liunx starts redis
1.14 - 流水线