diff --git a/0022-Generate-Parentheses/py-0022/Solution2.py b/0022-Generate-Parentheses/py-0022/Solution2.py new file mode 100644 index 00000000..0b32fa02 --- /dev/null +++ b/0022-Generate-Parentheses/py-0022/Solution2.py @@ -0,0 +1,29 @@ +# Source : https://leetcode.com/problems/generate-parentheses/ +# Author : penpenps +# Time : 2019-05-28 + +from typing import List + +# Traversal all combinations with left part length as i and right part as n-1-i +# Time Complexity: O(2^n) +# Space Complexity: O(n) + +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + ans = [] + for i in range(n): + left = self.generateParenthesis(i) + if not left: + left = [""] + right = self.generateParenthesis(n-1-i) + if not right: + right = [""] + for l in left: + for r in right: + ans.append("("+l+")"+r) + return ans + +if __name__ == '__main__': + solution = Solution() + n = 3 + print(solution.generateParenthesis(n)) \ No newline at end of file diff --git a/0022-Generate-Parentheses/py-0022/Solution3.py b/0022-Generate-Parentheses/py-0022/Solution3.py new file mode 100644 index 00000000..9d511ec6 --- /dev/null +++ b/0022-Generate-Parentheses/py-0022/Solution3.py @@ -0,0 +1,25 @@ +# Source : https://leetcode.com/problems/generate-parentheses/ +# Author : penpenps +# Time : 2019-05-28 + +from typing import List + +# Traversal all combinations with left part length as i and right part as n-1-i +# Time Complexity: O(2^n) +# Space Complexity: O(n) + +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + dp = [[] for _ in range(n+1)] + dp[0] = [""] + for i in range(1, n+1): + for j in range(i): + for l in dp[j]: + for r in dp[i-j-1]: + dp[i].append("("+l+")"+r) + return dp[n] + +if __name__ == '__main__': + solution = Solution() + n = 3 + print(solution.generateParenthesis(n)) \ No newline at end of file diff --git a/0075-Sort-Colors/py-0075/Solution1.py b/0075-Sort-Colors/py-0075/Solution1.py new file mode 100644 index 00000000..345f7e86 --- /dev/null +++ b/0075-Sort-Colors/py-0075/Solution1.py @@ -0,0 +1,33 @@ +# Source : https://leetcode.com/problems/sort-colors/ +# Author : penpenps +# Time : 2019-05-31 + +from typing import List + +# Counting and reset nums in order +# Time Complexity: O(2*n) +# Space Complexity: O(3) + +class Solution: + def sortColors(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + m = [0]*3 + for x in nums: + m[x] += 1 + + for i in range(len(nums)): + if i < m[0]: + nums[i] = 0 + if m[0] <= i < m[1] + m[0]: + nums[i] = 1 + if i >= m[0] + m[1]: + nums[i] = 2 + + +if __name__ == '__main__': + s = Solution() + nums = [2,0,2,1,1,0] + s.sortColors(nums) + print(nums) diff --git a/0075-Sort-Colors/py-0075/Solution2.py b/0075-Sort-Colors/py-0075/Solution2.py new file mode 100644 index 00000000..8cc2b940 --- /dev/null +++ b/0075-Sort-Colors/py-0075/Solution2.py @@ -0,0 +1,35 @@ +# Source : https://leetcode.com/problems/sort-colors/ +# Author : penpenps +# Time : 2019-05-31 + +from typing import List + +# Quick sort for 3 numbers +# Time Complexity: O(n) +# Space Complexity: O(3) + +class Solution: + def sortColors(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + zero = 0 + two = len(nums) - 1 + i = 0 + while i <= two: + if nums[i] == 0: + nums[i], nums[zero] = nums[zero], nums[i] + zero += 1 + i += 1 + elif nums[i] == 2: + nums[i], nums[two] = nums[two], nums[i] + two -= 1 + else: + i += 1 + + +if __name__ == '__main__': + s = Solution() + nums = [2,0,2,1,1,0] + s.sortColors(nums) + print(nums) diff --git a/0075-Sort-Colors/py-0075/Solution3.py b/0075-Sort-Colors/py-0075/Solution3.py new file mode 100644 index 00000000..01fefdb7 --- /dev/null +++ b/0075-Sort-Colors/py-0075/Solution3.py @@ -0,0 +1,32 @@ +# Source : https://leetcode.com/problems/sort-colors/ +# Author : penpenps +# Time : 2019-05-31 + +from typing import List + +# use [i, j) to keep 0, [j, k) keeps 1 and [k, ] keeps 2 +# Time Complexity: O(n) +# Space Complexity: O(4) + +class Solution: + def sortColors(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + i, j, k = 0, 0, 0 + for k in range(len(nums)): + v = nums[k] + nums[k] = 2 + if v < 2: + nums[j] = 1 + j += 1 + if v < 1: + nums[i] = 0 + i += 1 + + +if __name__ == '__main__': + s = Solution() + nums = [2,0,2,1,1,0] + s.sortColors(nums) + print(nums) diff --git a/readme.md b/readme.md index e9f8afa2..3cf8d7a1 100644 --- a/readme.md +++ b/readme.md @@ -113,7 +113,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 072 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | [solution](https://leetcode.com/problems/edit-distance/solution/) | [C++](0072-Edit-Distance/cpp-0072/) | | | | 073 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/description/) | [solution](https://leetcode.com/problems/set-matrix-zeroes/solution/) | [C++](0073-Set-Matrix-Zeroes/cpp-0073/) | | | | 074 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [无] | [C++](0074-Search-a-2D-Matrix/cpp-0074/) | | | -| 075 | [Sort Colors](https://leetcode.com/problems/sort-colors/description/) | [solution](https://leetcode.com/problems/sort-colors/solution/) | [C++](0075-Sort-Colors/cpp-0075/) | [Java](0075-Sort-Colors/java-0075/src/) | | +| 075 | [Sort Colors](https://leetcode.com/problems/sort-colors/description/) | [solution](https://leetcode.com/problems/sort-colors/solution/) | [C++](0075-Sort-Colors/cpp-0075/) | [Java](0075-Sort-Colors/java-0075/src/) | [Python](0075-Sort-Colors/py-0075/) | | 076 | [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/description/) | [solution](https://leetcode.com/problems/minimum-window-substring/solution/) | [C++](0076-Minimum-Window-Substring/cpp-0076/) | | | | 077 | [Combinations](https://leetcode.com/problems/combinations/description/) | [solution](https://leetcode.com/problems/combinations/solution/) | [C++](0077-Combinations/cpp-0077/) | [Java](0077-Combinations/java-0077/src/) | | | | | | | | |