-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.括号生成.cpp
43 lines (35 loc) · 849 Bytes
/
22.括号生成.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* @lc app=leetcode.cn id=22 lang=cpp
*
* [22] 括号生成
*/
// @lc code=start
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n==0)return {};
vector<string> res;
string track;
backtrack(n, n, track, res);
return res;
}
void backtrack(int left, int right, string& track, vector<string>& res){
if(right < left)return;
if(left<0 || right<0)return;
if(left==0 && right==0){
res.push_back(track);
return;
}
track.push_back('(');
backtrack(left-1,right,track,res);
track.pop_back();
track.push_back(')');
backtrack(left, right-1,track,res);
track.pop_back();
}
};
// @lc code=end