当前位置:网站首页>22 bracket generation
22 bracket generation
2022-07-24 15:51:00 【Nie Bingyu】
One 、 Preface
label : Backtracking algorithm .
Source of problem LeetCode 22 difficulty : secondary .
Question link :https://leetcode-cn.com/problems/generate-parentheses/
Two 、 subject
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 :
Input :n = 3
Output :[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
3、 ... and 、 Ideas
Backtracking solution , May refer to 《46 Total sort 》
Four 、 coded
//==========================================================================
/*
* @file : 022_GenerateParenthesis.h
* @label : Backtracking algorithm
* @blogs : https://blog.csdn.net/nie2314550441/article/details/107573657
* @author : niebingyu
* @date : 2020/07/25
* @title : 22. Bracket generation
* @purpose : 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 :
* Input :n = 3
* Output :[
* "((()))",
* "(()())",
* "(())()",
* "()(())",
* "()()()"
* ]
*
*
* source : Power button (LeetCode)
* difficulty : secondary
* link :https://leetcode-cn.com/problems/generate-parentheses/
*/
//==========================================================================
#pragma once
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <assert.h>
using namespace std;
#define NAMESPACE_GENERATEPARENTHESIS namespace NAME_GENERATEPARENTHESIS {
#define NAMESPACE_GENERATEPARENTHESISEND }
NAMESPACE_GENERATEPARENTHESIS
class Solution
{
public:
vector<string> generateParenthesis(int n)
{
vector<string> result;
string current;
backtrack(result, current, 0, 0, n);
return result;
}
private:
void backtrack(vector<string>& ans, string& cur, int open, int close, int n)
{
if (cur.size() == n * 2)
{
ans.push_back(cur);
return;
}
if (open < n)
{
cur.push_back('(');
backtrack(ans, cur, open + 1, close, n);
cur.pop_back();
}
if (close < open)
{
cur.push_back(')');
backtrack(ans, cur, open, close + 1, n);
cur.pop_back();
}
}
};
Here is the test code //
// test Use cases START
void test(const char* testName, int n, vector<string> expect)
{
Solution s;
vector<string> result = s.generateParenthesis(n);
if (result == expect)
cout << testName << ", solution passed." << endl;
else
cout << testName << ", solution failed. " << endl;
}
// The test case
void Test1()
{
int n = 3;
vector<string> expect =
{
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
};
test("Test1()", n, expect);
}
NAMESPACE_GENERATEPARENTHESISEND
// test Use cases END
//
void GenerateParenthesis_Test()
{
cout << "------ start 22. Bracket generation ------" << endl;
NAME_GENERATEPARENTHESIS::Test1();
cout << "------ end 22. Bracket generation --------" << endl;
}Execution results :

边栏推荐
- 每天20分钟之feign
- Experience summary of slow SQL problems
- PHP中array_merge的坑
- Still using listview? Use animatedlist to make list elements move
- 降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行
- Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]
- Who is the "roll" king of the prefabricated vegetable track?
- Citic securities account opening process, is it safe to open an account on your mobile phone
- Can flush accounts be opened directly? Is it safe to open an account? How to open an account??
- Power of leetcode 231.2
猜你喜欢

Adaptive design and responsive design

yolov3 训练自己的数据集

Leetcode 231. 2 的幂

matlab图像去雾技术GUI界面-全局平衡直方图

C# - partial 关键字

自适应设计和响应式设计

Force button 31. Next arrangement -- double finger needling

Knowledge points of MySQL (12)

Personal practical experience: Data Modeling "whether account data belongs to dimension or account domain"

Join parameter processing and @param
随机推荐
Dynamics 365: how to get the authentication information required to connect to D365 online from azure
Nine key measures to maintain server security in Hong Kong
Analysys analysis "2022 China data security market data monitoring report" was officially launched
Public and private key transmission, and understanding of CA certificate
Leetcode 231. 2 的幂
Adaptive design and responsive design
简化理解:发布订阅
MySQL source code analysis -- data structure of index
JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue
G026-db-gs-ins-03 openeuler deployment opengauss (1 active and 2 standby or multiple standby)
Multus of kubernetes multi network card scheme_ CNI deployment and basic use
Yolov6 trains its own data set
Azure key vault (1) Introduction
Istio1.12: installation and quick start
[acwing] 909. Chess game
YOLO5Face:为什么要重新发明人脸检测器
Dynamics 365: how to get the threshold value of executemullerequest in batch requests
从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
MySQL learning notes (summary)
With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!