当前位置:网站首页>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))
边栏推荐
- Share | defend against physically realizable image classification attacks
- 董明珠称“格力手机做得不比苹果差”哪里来的底气?
- NVIDIA SMI has failed because it could't communicate with the NVIDIA driver
- Cartographer learning records: 3D slam part of cartographer source code (I)
- JVM tuning 6: GC log analysis and constant pool explanation
- Simple linear regression of sklearn series
- KD-Tree and LSH
- AAAI2022-ShiftVIT: When Shift Operation Meets Vision Transformer
- Lr-link Lianrui fully understands the server network card
- How to apply for free idea with official documents
猜你喜欢

Huawei equipment is configured to access the virtual private network through GRE

The central rural work conference has released important signals. Ten ways for AI technology to help agriculture can be expected in the future

Pychart displays pictures with imshow

华为设备配置通过GRE接入虚拟专用网

Top 100 video information of station B

Deep search + backtracking

点击图标不灵敏咋整?

Simple linear regression of sklearn series

Vins fusion GPS fusion part

Tianchi - student test score forecast
随机推荐
The programmers of a large factory after 95 were dissatisfied with the department leaders, and were sentenced for deleting the database and running away
Lianrui electronics made an appointment with you with SIFA to see two network cards in the industry's leading industrial automation field first
Iris dataset - Introduction to machine learning
Huawei equipment is configured to access the virtual private network through GRE
Opencv learning path (2-2) -- Deep parsing namedwindow function
Huawei equipment configuration MCE
Topological sorting
点击图标不灵敏咋整?
Parametric contractual learning: comparative learning in long tail problems
[NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision
Section I: classification and classification of urban roads
Learning summary 01- machine learning
The data center is evolving towards the "four high" trend, and the OCP network card and the whole cabinet will be delivered into the mainstream of the future data center
Click the icon is not sensitive how to adjust?
[opencv learning problems] 1 Namedwindow() and imshow() show two windows in the picture
华为设备配置通过GRE接入虚拟专用网
C language test question 3 (advanced program multiple choice questions _ including detailed explanation of knowledge points)
Target detection - personal understanding of RCNN series
Yolov5 training personal data set summary
工具类ObjectUtil常用的方法