mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2026-01-25 07:34:05 +00:00
day 29: 22-generate-parentheses, 739-daily-temperatures
This commit is contained in:
12
22-generate-parentheses/driver.cpp
Normal file
12
22-generate-parentheses/driver.cpp
Normal 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;
|
||||
}
|
||||
24
22-generate-parentheses/soln.cpp
Normal file
24
22-generate-parentheses/soln.cpp
Normal 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;
|
||||
}
|
||||
8
22-generate-parentheses/soln.hpp
Normal file
8
22-generate-parentheses/soln.hpp
Normal 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);
|
||||
};
|
||||
Reference in New Issue
Block a user