From e963d61f8b0bf7b46dd242a7d49ab5b5e58883ae Mon Sep 17 00:00:00 2001 From: Shu Peng Date: Wed, 29 May 2019 00:42:32 +0800 Subject: [PATCH] Add Java and Python solution for problem #22 --- .../java-0022/src/Solution1.java | 40 +++++++++++++++++++ .../python-0022/Solution1.py | 29 ++++++++++++++ readme.md | 2 +- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 0022-Generate-Parentheses/java-0022/src/Solution1.java create mode 100644 0022-Generate-Parentheses/python-0022/Solution1.py diff --git a/0022-Generate-Parentheses/java-0022/src/Solution1.java b/0022-Generate-Parentheses/java-0022/src/Solution1.java new file mode 100644 index 00000000..93500f2e --- /dev/null +++ b/0022-Generate-Parentheses/java-0022/src/Solution1.java @@ -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 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 generateParenthesis(int n) { + List ans = new ArrayList<>(); + helper(ans, "", 0, 0, n); + return ans; + } + + private static void printList(List list){ + for(String e: list) + System.out.print(e + " "); + System.out.println(); + } + + public static void main(String[] args) { + int n = 3; + List res = (new Solution1()).generateParenthesis(n); + printList(res); + } +} diff --git a/0022-Generate-Parentheses/python-0022/Solution1.py b/0022-Generate-Parentheses/python-0022/Solution1.py new file mode 100644 index 00000000..9c0046e9 --- /dev/null +++ b/0022-Generate-Parentheses/python-0022/Solution1.py @@ -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)) \ No newline at end of file diff --git a/readme.md b/readme.md index 0d55a5f9..bcb6ef07 100644 --- a/readme.md +++ b/readme.md @@ -66,7 +66,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 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/) | | |