Skip to content

Commit 58d7468

Browse files
committed
Time: 1 ms (66.1%), Space: 54.7 MB (60.51%) - LeetHub
source:040d9e9330fbce805277af133e6627c473995001
1 parent a259325 commit 58d7468

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
var generateParenthesis = function(n) {
6+
const result = [];
7+
8+
function backtrack(open, close, path) {
9+
if (path.length === 2 * n) {
10+
result.push(path);
11+
return;
12+
}
13+
14+
if (open < n) {
15+
backtrack(open + 1, close, path + "(");
16+
}
17+
if (close < open) {
18+
backtrack(open, close + 1, path + ")");
19+
}
20+
}
21+
22+
backtrack(0, 0, "");
23+
return result;
24+
};

0 commit comments

Comments
 (0)