Skip to content

Commit 8423603

Browse files
committed
Create 22.括号生成.js
1 parent f29f437 commit 8423603

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

22.括号生成.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
var generateParenthesis = function(n) {
6+
const ans = [];
7+
function walk(str, l, r) {
8+
if (l >= n && r >= n) {
9+
ans.push(str);
10+
return;
11+
}
12+
if (l <= r) {
13+
walk(str + '(', l + 1, r);
14+
} else {
15+
if (l < n) {
16+
walk(str + '(', l + 1, r);
17+
}
18+
if (r < n) {
19+
walk(str + ')', l, r + 1);
20+
}
21+
}
22+
}
23+
walk('', 0, 0);
24+
return ans;
25+
};

0 commit comments

Comments
 (0)