Skip to content

Commit

Permalink
Add Java and Python solution for problem liuyubobobo#22
Browse files Browse the repository at this point in the history
  • Loading branch information
penpenps authored and Shu Peng committed May 28, 2019
1 parent 91d90d6 commit e963d61
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
40 changes: 40 additions & 0 deletions 0022-Generate-Parentheses/java-0022/src/Solution1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// Source : https://leetcode.com/problems/generate-parentheses/
/// Author : penpenps
/// Time : 2019-05-28

import java.util.*;

/// Recursive with "(" and ")" separately and guarantee open and close num is equal
/// Time Complexity: O(2^n)
/// Space Complexity: O(n)

public class Solution1 {
public void helper(List<String> ans, String str, int open, int close, int max) {
if(str.length() == 2*max){
ans.add(str);
return;
}

if(open < max)
helper(ans, str+"(", open+1, close, max);
if(close < open)
helper(ans, str+")", open, close+1, max);
}
public List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList<>();
helper(ans, "", 0, 0, n);
return ans;
}

private static void printList(List<String> list){
for(String e: list)
System.out.print(e + " ");
System.out.println();
}

public static void main(String[] args) {
int n = 3;
List<String> res = (new Solution1()).generateParenthesis(n);
printList(res);
}
}
29 changes: 29 additions & 0 deletions 0022-Generate-Parentheses/python-0022/Solution1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Source : https://leetcode.com/problems/generate-parentheses/
# Author : penpenps
# Time : 2019-05-28

from typing import List

# Recursive with "(" and ")" separately and guarantee open and close num is equal
# Time Complexity: O(2^n)
# Space Complexity: O(n)

class Solution:
def generateParenthesis(self, n: int) -> List[str]:
def helper(current, openNum, closeNum, n, ans):
if len(current) == 2*n:
ans.append(current)
return
if openNum < n:
helper(current+"(", openNum + 1, closeNum, n, ans)
if closeNum < openNum:
helper(current+")", openNum, closeNum + 1, n, ans)

ans = []
helper("", 0, 0, n, ans)
return ans

if __name__ == '__main__':
solution = Solution()
n = 3
print(solution.generateParenthesis(n))
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ email: [[email protected]](mailto:[email protected])
| 019 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) | [solution](https://leetcode.com/problems/remove-nth-node-from-end-of-list/solution/) | [C++](0019-Remove-Nth-Node-From-End-of-List/cpp-0019/) | [Java](0019-Remove-Nth-Node-From-End-of-List/java-0019/src/) | |
| 020 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/) | [solution](https://leetcode.com/problems/valid-parentheses/solution/) | [C++](0020-Valid-Parentheses/cpp-0020/) | [Java](0020-Valid-Parentheses/java-0020/src/) | |
| 021 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/) | [solution](https://leetcode.com/problems/merge-two-sorted-lists/solution/) | [C++](0021-Merge-Two-Sorted-Lists/cpp-0021/) | | |
| 022 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/description/) | [solution](https://leetcode.com/problems/generate-parentheses/solution/) | [C++](0022-Generate-Parentheses/cpp-0022/) | | |
| 022 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/description/) | [solution](https://leetcode.com/problems/generate-parentheses/solution/) | [C++](0022-Generate-Parentheses/cpp-0022/) | [Java](0022-Generate-Parentheses/java-0022/) | [Python](0022-Generate-Parentheses/python-0022/) |
| 023 | [Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/description/) | [solution](https://leetcode.com/problems/merge-k-sorted-lists/solution/) | [C++](0023-Merge-k-Sorted-Lists/cpp-0023/) | | |
| 024 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/description/) | [] | [C++](0024-Swap-Nodes-in-Pairs/cpp-0024/) | [Java](0024-Swap-Nodes-in-Pairs/java-0024/src/) | |
| 025 | [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/description/) | [] | [C++](0025-Reverse-Nodes-in-k-Group/cpp-0025/) | | |
Expand Down

0 comments on commit e963d61

Please sign in to comment.