当前位置:网站首页>22、生成括号
22、生成括号
2022-06-11 05:08:00 【qq_26391203】

/* * 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。 * 有效的括号: * 输入:n = 3 * 输出:["((()))","(()())","(())()","()(())","()()()"] * 输入:n = 1 * 输出:["()"] * * * 从模拟的过程: * 首先串为空,既可以添加(、也可以添加) * 什么时候迭代添加结束,即str的长度为2n,或者无效时; * 什么时候无效? 即左括号的个数比右括号少,其他情况都可以继续迭代 * 继续迭代时,时候时候可以加(,什么时候可以加) * (的个数和n相关,)的个数和左括号的个数相关,应该满足:r<=l<=n,=>l<n,r<l是迭代的基础 * * 难点:抽象出左右括号的关系,和迭代的基础 * */
var generateParenthesis = function(n) {
var result = [],l=0,r=0,str='';
back_tracking(n,result,l,r,str);
return result;
};
var back_tracking=function(n,result,l,r,str){
if(l<r){
return;
}
if(str.length===2*n){
result.push(str);
return;
}
if(l<n){
back_tracking(n,result,l+1,r,str+'(');
}
if(l>=r){
back_tracking(n,result,l,r+1,str+')');
}
}
console.log(generateParenthesis(1))
边栏推荐
- Anaconda installation and use process
- mysql字符串转数组,合并结果集,转成数组
- jvm调优五:jvm调优工具和调优实战
- Comparison of gigabit network card chips: who is better, a rising star or a Jianghu elder?
- How to apply for free idea with official documents
- Combien de courant le câblage des PCB peut - il supporter?
- 力扣(LeetCode)161. 相隔为 1 的编辑距离(2022.06.10)
- Huawei device configuration bgp/mpls IP virtual private network command
- 选择数字资产托管人时,要问的 6 个问题
- 华为设备配置BGP/MPLS IP 虚拟专用网
猜你喜欢

Lr-link Lianrui fully understands the server network card

Iris dataset - Introduction to machine learning

Linked list de duplication

点击图标不灵敏咋整?

Reverse thinking: making cartoon photos real

Introduction to coordinate system in navigation system
![[Transformer]On the Integration of Self-Attention and Convolution](/img/64/59f611533ebb0cc130d08c596a8ab2.jpg)
[Transformer]On the Integration of Self-Attention and Convolution

Tightly coupled laser vision inertial navigation slam system: paper notes_ S2D. 66_ ICRA_ 2021_ LVI-SAM

Huawei equipment is configured with bgp/mpls IP virtual private network

Restoration of binary tree -- number restoration
随机推荐
How to purchase 25g optical network card
Section II: structural composition characteristics of asphalt pavement (2) structural layer and performance requirements
Combien de courant le câblage des PCB peut - il supporter?
Differences between the four MQ
Huawei equipment configures local virtual private network mutual access
Lianrui electronics made an appointment with you with SIFA to see two network cards in the industry's leading industrial automation field first
Retinanet+keras train their own data set to tread on the pit
Deep extension technology: intelligent OCR recognition technology based on deep learning has great potential
NVIDIA SMI has failed because it could't communicate with the NVIDIA driver
Reverse thinking: making cartoon photos real
Opencv learning path (2-1) -- Deep parsing imread function
四大MQ的区别
Introduction to coordinate system in navigation system
Let me tell you how to choose a 10 Gigabit network card
Simple knowledge distillation
Huawei device configuration bgp/mpls IP virtual private network command
Concurrent search set
Take stock of the AI black technologies in the Beijing Winter Olympic Games, and Shenzhen Yancheng Technology
[markdown syntax advanced] make your blog more exciting (III: common icon templates)
Poverty has nothing to do with suffering