当前位置:网站首页>括号生成---2022/02/25
括号生成---2022/02/25
2022-06-11 17:18:00 【城猪猪】
题目描述
数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
题目来源
解题思路
首先补充一下关于深度优先搜索与广度优先搜索的知识。
深度优先搜索相当于树的先序遍历(从根结点出发,先序遍历左子树,而后先序遍历右子树),通常使用栈的数据存储观念进行计算;广度优先搜索相当于树的层次遍历,通常使用队列的数据存储观念进行计算。
代码实现
class Solution {
List<String> res=new ArrayList<>();
public List<String> generateParenthesis(int n) {
dfs(n,n,"");
return res;
}
public void dfs(int left,int right,String curStr){
if(left==0&&right==0)
{
res.add(curStr);
return;
}
if(left>0){
dfs(left-1,right,curStr+"(");
}
if(right>left){
dfs(left,right-1,curStr+")");
}
}
}
性能评估

暂时没考虑到优化的方向。
边栏推荐
- 6-8 有结构文件的读写1
- QLineEdit 设置输入掩码
- Custom or subscription? What is the future development trend of China's SaaS industry?
- 为什么udp流设置1316字节
- [online problem] timeout waiting for connection from pool
- 搜狐全員遭詐騙,暴露哪些問題?
- 端口规划与APJ
- Is the second-class cost engineer worth the exam? What is the development prospect?
- Environment configuration and pymysql installation
- Read and understand the development plan for software and information technology service industry during the "14th five year plan"
猜你喜欢

二级造价工程师值得考吗?发展前景如何?

10 times faster than 5g. Are you ready for 10 Gigabit communication?

活动 | Authing 首次渠道合作活动圆满落幕

Chapter II relational database

子类继承了什么、多态、 向上转型

Dynamic: capturing network dynamics using dynamic graph representation learning

How to become an optimist organization?

What problems are exposed when all Sohu employees are cheated?

Authing 双周动态:应用市场上线(5 .10 —5 .22 )
![[pytest learning] after the pytest case fails to execute, the others will not be executed](/img/c7/52ae88cde65bdd12ae8b86df5c477f.png)
[pytest learning] after the pytest case fails to execute, the others will not be executed
随机推荐
Analyze which should be tested in PMP and ACP with actual cases? Which is more useful?
Port planning and APJ
Real time myth -- real-time RTOS multitask performance analysis
LeetCode-384. 打乱数组
说说集合的面试题
Format eslint automatique lorsque vscode enregistre le Code
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
What subclasses inherit, polymorphism, and upward transformation
Xie Yang, CEO of authing, was selected into Forbes' 30 under 30 Asia list in 2021
From a "trendsetter" to a "wind chaser", can master Kang still lead the market?
vscode配置eslint自动格式化报错“The setting is deprecated. Use editor.codeActionsOnSave instead with a source“
Katalon Studio Enterprise
QLineEdit 设置输入掩码
Arraylist集合、对象数组
CS0006 C# 未能找到元数据文件“C:\Users\...问题
Global and China Mobile Network Optimization (MnO) industry development panoramic survey and Investment Strategy Research Report 2022-2028
C语言:使用.h和.c文件遇到的问题总结
6-5 统计单词数量(文件)(*)
Recyclerview cache reuse analysis, source code interpretation
【线上问题】Timeout waiting for connection from pool 问题排查