当前位置:网站首页>5. bracket generation
5. bracket generation
2022-06-09 11:52:00 【Yuan Sheng Xiao 196】
subject :
Numbers
nRepresents the logarithm of the generated bracket , Please design a function , Used to be able to generate all possible and Effective Bracket combination .

Their thinking ( to flash back ):
Incoming numbers n Represents logarithm , This n The branching options that can be thought of as parentheses are from 0 To n,n As many as there are n+1 Secondary selection . Also make sure that the returned pair of parentheses is correct , Then the right parenthesis must be less than or equal to the number of left parentheses , Can continue to run , So if the right parenthesis is larger than the left parenthesis, you can return directly , There is no need to go any further , Only one parenthesis is added to the recursive function at a time , After the addition, it is handed over to the next recursive function for execution , If the left bracket equals the right bracket equals n You can put characters in the returned list to return

Source code :
public IList<string> GenerateParenthesis(int n)
{
// Define a table to store the correct string
IList<string> result = new List<string>();
backTracking(n, result, 0, 0, "");
return result;
}
private void backTracking(int n,IList<string> result,int left,int right,string str)
{
// If the right bracket is larger than the left bracket, return
if (right > left)
return;
// If the number of left parentheses is equal to the number of right parentheses is equal to n The number of indicates that the string is in compliance with the regulations , Just add him back List in
if(left == right && right == n)
{
result.Add(str);
return;
}
// If the left bracket is smaller than n join (
if(left < n)
backTracking(n, result, left + 1, right, str + "(");
// If the closing bracket is smaller than the closing bracket, add )
if (right < left)
backTracking(n, result, left, right + 1, str + ")");
}Function diagram :

边栏推荐
- 如何解决存在的1px问题?
- Focus on DNS: analysis of ISC bind
- win10安装过程
- 苹果称M2比intel i5强26倍 虚假营销的实情揭晓!
- Prompt credssp encryption database correction when windows is remote
- Computer quick index query software listary
- 【数据中台】00丨开篇词丨数据中台,是陷阱?还是金钥匙?
- How to make money through Hongmeng ecology?
- How to solve the existing 1px problem?
- About CSP and its implementation
猜你喜欢

5.括号生成

Actual combat of Nacos configuration center, standard components of Pangu microservice development

08 | 中台落地第三步:中台的规划与设计(Design)

【无标题】
![[数据分析] RFM分析方法](/img/74/20632b1aaacd71336b4f750dc1c057.png)
[数据分析] RFM分析方法

Win7系统怎么卸载IE浏览器

How to solve the existing 1px problem?

Use of component El scrollbar

建议收藏:数据标准的概念,分类,价值及6大实施步骤解析

How to uninstall IE browser in win7 system
随机推荐
消防工程师与消防员有什么区别?
2022年软考网络工程师考试备考指南
Open IPv6 access on the website to test whether it supports IPv6
go-zero 微服务实战系列(二、服务拆分)
[buuctf.reverse] 111 [b01lers2020] chugga Chugga
06 | 中台落地第一步:企业战略分解及现状调研(Discovery)
Win10 20h2 was officially released, and the list of old and new features was compared
[buuctf.reverse] 104_ [FlareOn1]Shellolololol
H3C认证网络工程师
Computer quick index query software listary
给DNS划重点:分析ISC BIND必知必会
分段、分页、页表、快表学习笔记
(critical) chassis-frontend. c:122: Failed to get log directory, please set by --log-path
6.两两交换链表中的节点
How to uninstall IE browser in win7 system
VMware vSphere 6.5 configuration family
2021年下半年系统集成项目管理工程师案例分析真题及答案解析
How to insert the video monitoring memory card into the Yuntai pro of Xiaomi smart camera
详解PCB线路板覆铜基础知识
08 | 中台落地第三步:中台的规划与设计(Design)