当前位置:网站首页>[record of question brushing] 22. bracket generation
[record of question brushing] 22. bracket generation
2022-07-26 20:27:00 【InfoQ】
One 、 Title Description
Input :n = 3
Output :["((()))","(()())","(())()","()(())","()()()"]
Input :n = 1
Output :["()"]
- 1 <= n <= 8
II. Train of thought analysis
backtracking nn(n)2n- The number of left parentheses shall not be greater than
n, We can put a left bracket .
- If the number of right brackets is less than the number of left brackets , Let's put a right parenthesis . To ensure that our parentheses are paired and valid .
3、 ... and 、 Code implementation
class Solution {
public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<String>();
dfs(res, new StringBuilder(), 0, 0, n);
return res;
}
public void dfs(List<String> ans, StringBuilder cur, int l, int r, int len) {
if (cur.length() == len * 2) {
ans.add(cur.toString());
return;
}
if (l < len) {
cur.append('(');
dfs(ans, cur, l + 1, r, len);
cur.deleteCharAt(cur.length() - 1);
}
if (r < l) {
cur.append(')');
dfs(ans, cur, l, r + 1, len);
cur.deleteCharAt(cur.length() - 1);
}
}
}
Complexity analysis
- Time complexity : The typical Cartland number problem , The complexity is
nNumber of left parentheses .
- Spatial complexity :
Running results

summary
边栏推荐
- This points to the simplest rule remember it
- STM32F103有源蜂鸣器驱动程序
- C # convert PDF files into pictures
- 解决AttributeError: module ‘win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu
- An open source web drawing board is really convenient
- Principle and application of one click login of local number (glory Collection Edition)
- Kotlin - 协程构建器 CoroutineBuilder
- Shell script basic programming commands
- 静音台式风扇芯片-DLTAP703SD-杰力科创
- 员工辞职还得赔偿公司损失?34岁机长辞职被公司索赔1066万
猜你喜欢

Using questpdf operation to generate PDF is faster and more efficient!

SwiftUI 4 新功能之实时获取点击位置 .onTapGesture { location in} (教程含源码)

消息队列知识点总结

App Uploader下载安装

20220725 tree array introspection

C # use the default transformation method

超强接口协作平台如何打造:细数Apifox的六把武器

gospel! Wechat personal official account can be renamed!

Record an analysis of a.Net property management background service stuck
![Ue5 editor slate quick start [opening]](/img/88/9c6e8ff970b3138299ecbe79d90f2e.png)
Ue5 editor slate quick start [opening]
随机推荐
Parallel execution (II). Multiprocessing
Use request header authentication to test API interfaces that need authorization
医疗直播平台需要什么功能
SwiftUI 4 新功能之实时获取点击位置 .onTapGesture { location in} (教程含源码)
Silent desktop fan chip dltap703sd Jericho
This points to the simplest rule remember it
this指向-超经典面试题
记一次 .NET 某物管后台服务 卡死分析
.net GC workflow
C # use the default transformation method
How to wait for the return results of multiple asynchronous tasks synchronously?
安全测试初学体验
C#异步编程看这篇就够了
会议OA之会议排座&送审
Impersonate authentication
T246836 [lsot-1] potatoes of Tyrannosaurus Rex
Introduction to component functions of blueprism process business object Chapter 3 of RPA
Definition and use of one-dimensional array
The Sandbox 和艺术家 Alec Monopoly 达成合作
This point - super classic interview questions