Skip to content

Commit e5081f7

Browse files
committed
solve problem Generate Parentheses
1 parent e22340c commit e5081f7

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ All solutions will be accepted!
199199
|867|[Transpose Matrix](https://leetcode-cn.com/problems/transpose-matrix/description/)|[java/py/js](./algorithms/TransposeMatrix)|Easy|
200200
|479|[Largest Palindrome Product](https://leetcode-cn.com/problems/largest-palindrome-product/description/)|[java/py/js](./algorithms/LargestPalindromeProduct)|Easy|
201201
|102|[Binray Tree Level Order Traversal](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/)|[java/py/js](./algorithms/BinrayTreeLevelOrderTraversal)|Medium|
202+
|22|[Generate Parentheses](https://leetcode-cn.com/problems/generate-parentheses/description/)|[java/py/js](./algorithms/GenerateParentheses)|Easy|
202203

203204
# Database
204205
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Generate Parentheses
2+
This problem can solve by Backtracking Algorithm
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public List<String> generateParenthesis(int n) {
3+
List<String> res = new ArrayList<String>();
4+
backTrack("", n, n, res);
5+
return res;
6+
}
7+
8+
public void backTrack(String s, int left, int right, List<String> res) {
9+
if (left == 0 && right == 0) {
10+
res.add(s);
11+
return;
12+
}
13+
if (left > 0) {
14+
backTrack(s + "(", left - 1, right, res);
15+
}
16+
if (left < right) {
17+
backTrack(s + ")", left, right - 1, res);
18+
}
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
var generateParenthesis = function(n) {
6+
let res = []
7+
backTrack('', n, n, res)
8+
return res
9+
};
10+
11+
var backTrack = function(s, left, right, res) {
12+
if (left === 0 && right === 0) {
13+
res.push(s)
14+
return
15+
}
16+
if (left > 0) {
17+
backTrack(s + '(', left - 1, right, res)
18+
}
19+
if (left < right) {
20+
backTrack(s + ')', left, right - 1, res)
21+
}
22+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def generateParenthesis(self, n):
3+
"""
4+
:type n: int
5+
:rtype: List[str]
6+
"""
7+
res = []
8+
self.backtrack('', n, n, res)
9+
return res
10+
11+
def backtrack(self, s, left, right, res):
12+
if left == 0 and right == 0:
13+
res.append(s)
14+
return
15+
16+
if left > 0:
17+
self.backtrack(s + '(', left - 1, right, res)
18+
if left < right:
19+
self.backtrack(s + ')', left, right - 1, res)

0 commit comments

Comments
 (0)