Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1377b9b

Browse files
Daily Solutions With JS
1 parent 9d599fa commit 1377b9b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎Medium/22. Generate Parentheses.js‎

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /