forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java and Python solution for problem liuyubobobo#22
- Loading branch information
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/) | | | | ||
|