Created
May 10, 2019 09:34
-
-
Save bunnyadad/1d8f13ed9401a3dae1ad2d52eaef0310 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
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
12 ms | 14 MB