Skip to content

Commit

Permalink
Add solutions for #0075 and append two more solutions for liuyubobobo#22
Browse files Browse the repository at this point in the history
  • Loading branch information
Shu Peng committed Jun 2, 2019
1 parent bdcbc76 commit e0d2aa2
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 1 deletion.
29 changes: 29 additions & 0 deletions 0022-Generate-Parentheses/py-0022/Solution2.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

# 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))
25 changes: 25 additions & 0 deletions 0022-Generate-Parentheses/py-0022/Solution3.py
Original file line number Diff line number Diff line change
@@ -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))
33 changes: 33 additions & 0 deletions 0075-Sort-Colors/py-0075/Solution1.py
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions 0075-Sort-Colors/py-0075/Solution2.py
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions 0075-Sort-Colors/py-0075/Solution3.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ email: [[email protected]](mailto:[email protected])
| 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/) | |
| | | | | | |
Expand Down

0 comments on commit e0d2aa2

Please sign in to comment.