Skip to content

Commit 5870c22

Browse files
author
Chi Hi
committed
Add parentheses generator
1 parent d4d1303 commit 5870c22

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎Backtracking/Algorithms_ToDo.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
+ Valid Sudoku [LeetCode #36](https://leetcode.com/problems/valid-sudoku/)
44
+ Regular Expression Matching [LeetCode #10](https://leetcode.com/problems/regular-expression-matching/)
55
+ Letter Combination of a Phone Number [LeetCode #17](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)
6-
+ Generate Parentheses [LeetCode #22](https://leetcode.com/problems/generate-parentheses/)
76
+ Combination Sum [LeetCode #39](https://leetcode.com/problems/combination-sum/)
87
+ Wildcard Matching [LeetCode #44](https://leetcode.com/problems/wildcard-matching/)
98
+ N-Queens [LeetCode #51](https://leetcode.com/problems/n-queens/)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
* [
5+
"((()))",
6+
"(()())",
7+
"(())()",
8+
"()(())",
9+
"()()()"
10+
]
11+
*/
12+
const generateParenthesis = function(n) {
13+
if (n === 0) {
14+
return [""];
15+
}
16+
let result = [];
17+
for (let i = 0; i < n; ++i) {
18+
let lefts = generateParenthesis(i);
19+
let rights = generateParenthesis(n - i - 1);
20+
21+
for (let l = 0; l < lefts.length; ++l) {
22+
for (let r = 0; r < rights.length; ++r) {
23+
result.push("(" + lefts[l] + ")" + rights[r]);
24+
}
25+
}
26+
}
27+
return result;
28+
};
29+
30+
console.log(generateParenthesis(3));

0 commit comments

Comments
 (0)