Skip to content

Instantly share code, notes, and snippets.

@bunnyadad
Created May 10, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunnyadad/1d8f13ed9401a3dae1ad2d52eaef0310 to your computer and use it in GitHub Desktop.
Save bunnyadad/1d8f13ed9401a3dae1ad2d52eaef0310 to your computer and use it in GitHub Desktop.
class Solution
{
public:
vector<string> generateParenthesis(int n)
{
vector<string> rst;
string str;
gpdfs(n, n, str, rst);
return rst;
}
void gpdfs(int left, int right, string str, vector<string>& rst)
{
if (left == 0 && right == 0)
{
rst.push_back(str);
return;
}
else if (left > right)
return;
else
{
if (left > 0) gpdfs(left - 1, right, str + "(", rst);
if (right > 0) gpdfs(left, right - 1, str + ")", rst);
}
return;
}
};
@bunnyadad
Copy link
Author

12 ms | 14 MB

@bunnyadad
Copy link
Author

Runtime: 4 ms, faster than 99.68% of C++ online submissions for Generate Parentheses.
Memory Usage: 17.4 MB, less than 34.10% of C++ online submissions for Generate Parentheses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment