-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwithString.h
More file actions
43 lines (33 loc) · 831 Bytes
/
Copy pathwithString.h
File metadata and controls
43 lines (33 loc) · 831 Bytes
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
43
/*
* leetcode string test
*/
#ifndef WITHSTRING_H_
#define WITHSTRING_H_
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
class Solution {
public:
// Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
//
// For example, given n = 3, a solution set is:
//
// "((()))", "(()())", "(())()", "()(())", "()()()"
vector<string> generateParenthesis(int n) {
vector<string> res;
addingpar(res, "", n, 0);
return res;
}
void addingpar(vector<string> &v, string str, int n, int m){
if(n==0 && m==0) {
v.push_back(str);
return;
}
if(m > 0){ addingpar(v, str+")", n, m-1); }
if(n > 0){ addingpar(v, str+"(", n-1, m+1); }
}
};
#endif /* WITHSTRING_H_ */