day 29: 22-generate-parentheses, 739-daily-temperatures

This commit is contained in:
2023-10-30 12:16:48 -07:00
parent 392b6c97e9
commit 34446fb0a9
6 changed files with 106 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
#include "soln.hpp"
int main()
{
int n = 4;
Solution soln;
vector<string> result = soln.generateParenthesis(n);
for (string str : result)
cout << str << "\t";
cout << endl;
return 0;
}

View File

@@ -0,0 +1,24 @@
#include "soln.hpp"
void Solution::recurseParenthesis(vector<string> &result, string str, int left, int right)
{
// base case
if (left == 0 & right == 0)
{
result.push_back(str);
return;
}
// opening parenthesis can be added, so add
if (left > 0)
recurseParenthesis(result, str + "(", left - 1, right);
// closing parenthesis can be added, so add
if (right > left)
recurseParenthesis(result, str + ")", left, right - 1);
}
vector<string> Solution::generateParenthesis(int n)
{
vector<string> result;
recurseParenthesis(result, "", n, n);
return result;
}

View File

@@ -0,0 +1,8 @@
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void recurseParenthesis(vector<string>& result, string str, int left, int right);
vector<string> generateParenthesis(int n);
};