当前位置:网站首页>22. Generate parentheses
22. Generate parentheses
2022-06-11 05:17:00 【qq_ twenty-six million three hundred and ninety-one thousand tw】

/* * 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 . * Valid parenthesis : * Input :n = 3 * Output :["((()))","(()())","(())()","()(())","()()()"] * Input :n = 1 * Output :["()"] * * * From the process of simulation : * First, the string is empty , You can add (、 You can also add ) * When the iteration addition ends , namely str The length of is 2n, Or invalid ; * When is it invalid ? That is, the number of left parentheses is less than that of right parentheses , In other cases, you can continue to iterate * When continuing the iteration , Sometimes you can add (, When can I add ) * ( The number of and n relevant ,) The number of is related to the number of left parentheses , Should be satisfied with :r<=l<=n,=>l<n,r<l Is the foundation of iteration * * difficulty : Abstract the relationship between the left and right brackets , And the basis of iteration * */
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))
边栏推荐
- Lianrui: how to rationally see the independent R & D of domestic CPU and the development of domestic hardware
- 华为设备配置MCE
- 一大厂95后程序员对部门领导不满,删库跑路被判刑
- Vins fusion GPS fusion part
- C (I) C basic grammar all in one
- Tianchi - student test score forecast
- 博途仿真时出现“没有针对此地址组态任何硬件,无法进行修改”解决办法
- Lianrui electronics made an appointment with you with SIFA to see two network cards in the industry's leading industrial automation field first
- C language test question 3 (advanced program multiple choice questions _ including detailed explanation of knowledge points)
- JVM tuning 6: GC log analysis and constant pool explanation
猜你喜欢

New product pre-sale: 25g optical network card based on Intel 800 series is coming

QT Road (2) -- HelloWorld

Click the icon is not sensitive how to adjust?

推荐一款免费的内网穿透开源软件,可以在测试本地开发微信公众号使用

Zed2 camera manual

Share | defend against physically realizable image classification attacks

Oh my Zsh correct installation posture

Linked list de duplication

(十五)红外通信

jvm调优五:jvm调优工具和调优实战
随机推荐
Deep extension technology: intelligent OCR recognition technology based on deep learning has great potential
Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
Share | guide language image pre training to achieve unified visual language understanding and generation
Huawei equipment configuration MCE
Carrier coordinate system inertial coordinate system world coordinate system
What is the difference between gigabit network card and 10 Gigabit network card?
AAAI2022-ShiftVIT: When Shift Operation Meets Vision Transformer
2021 iccv paper sharing - occlusion boundary detection
Network adapter purchase guide
PCB走線到底能承載多大電流
一大厂95后程序员对部门领导不满,删库跑路被判刑
lower_ Personal understanding of bound function
Deep search + backtracking
华为设备配置BGP/MPLS IP 虚拟专用网
Emnlp2021 𞓜 a small number of data relation extraction papers of deepblueai team were hired
截取文件扩展名
使用acme.sh自动申请免费SSL证书
Differences between the four MQ
IOU series (IOU, giou, Diou, CIO)
华为设备配置MCE