当前位置:网站首页>【leetcode】22. bracket-generating
【leetcode】22. bracket-generating
2022-07-06 03:56:00 【Chinese fir sauce_】
subject :
22. Bracket generation
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 :["()"]
Tips :
1 <= n <= 8
Depth-first traversal :
class Solution {
List<String> ans;
public List<String> generateParenthesis(int n) {
ans = new ArrayList<>();
search(0,0,n,0,"");
return ans;
}
void search(int ln,int rn,int n,int len,String cur){
if(len == n * 2){
ans.add(new String(cur));
return;
}
// The number of left parentheses is less than n
if(ln < n){
search(ln+1,rn,n,len+1,cur+"(");
}
// The number of right parentheses is less than that of left parentheses
if(rn < ln){
search(ln,rn+1,n,len+1,cur+")");
}
}
}
边栏推荐
- Take you to wechat applet development in 3 minutes
- JVM的手术刀式剖析——一文带你窥探JVM的秘密
- RT thread -- FTP of LwIP (2)
- Suggestions for new engineer team members
- [matlab] - draw a five-star red flag
- Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
- C mouse event and keyboard event of C (XXVIII)
- 阿里测试师用UI自动化测试实现元素定位
- Python book learning notes - Chapter 09 section 01 create and use classes
- 登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
猜你喜欢
Blue Bridge Cup - day of week
C form application of C (27)
RT thread -- FTP of LwIP (2)
Cf464e the classic problem [shortest path, chairman tree]
C#(三十)之C#comboBox ListView treeView
BUAA calculator (expression calculation - expression tree implementation)
Record the pit of NETCORE's memory surge
mysql从一个连续时间段的表中读取缺少数据
KS008基于SSM的新闻发布系统
20、 EEPROM memory (AT24C02) (similar to AD)
随机推荐
Facebook等大厂超十亿用户数据遭泄露,早该关注DID了
【按键消抖】基于FPGA的按键消抖模块开发
C#(二十九)之C#listBox checkedlistbox imagelist
2.2 STM32 GPIO operation
3.2 detailed explanation of rtthread serial port device (V2)
Basic concepts of LTE user experience
判断当天是当月的第几周
Blue Bridge Cup - Castle formula
Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did
[Qt5] QT QWidget immediately appears and disappears
Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control
Factors affecting user perception
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
/usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
Flask learning and project practice 9: WTF form verification
Benefits of automated testing
[American competition] mathematical terms
Prime protocol announces cross chain interconnection applications on moonbeam
在 .NET 6 中使用 Startup.cs 更简洁的方法
The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched