Skip to content

Commit 42248f5

Browse files
committed
1221
1 parent f556bf0 commit 42248f5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| [JavaScript](./algorithms/Remove Nth Node From End of List.js)|Easy|
2424
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [JavaScript](./algorithms/Valid Parentheses.js)|Easy|
2525
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)| [JavaScript](./algorithms/Merge Two Sorted Lists.js)|Easy|
26+
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [JavaScript](./algorithms/Generate Parentheses.js)|Medium|
2627
|371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/)| [JavaScript](./algorithms/Sum of Two Integers.js)|Easy|
2728
|372|[Super Pow](https://leetcode.com/problems/super-pow/)| [JavaScript](./algorithms/SuperPow.js)|Medium|
2829

algorithms/Generate Parentheses.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
var add=function(left,right,stack,s){
6+
if(left===0 && right===0){
7+
stack.push(s);
8+
return
9+
}
10+
if(left===right){
11+
add(left-1,right,stack,s+"(");
12+
}
13+
else if(left===0){
14+
add(left,right-1,stack,s+")");
15+
}
16+
else{
17+
add(left-1,right,stack,s+"(");
18+
add(left,right-1,stack,s+")");
19+
}
20+
}
21+
22+
var generateParenthesis = function(n) {
23+
var stack=[];
24+
add(n,n,stack,"");
25+
return stack;
26+
};

0 commit comments

Comments
 (0)