Skip to content

Commit 35ac047

Browse files
committed
python_169
1 parent 99a6423 commit 35ac047

6 files changed

+167
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
3+
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
4+
5+
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
6+
7+
Given the following binary tree:  root = [3,5,1,6,2,0,8,null,null,7,4]
8+
9+
10+
 
11+
12+
Example 1:
13+
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
14+
Output: 3
15+
Explanation: The LCA of nodes 5 and 1 is 3.
16+
17+
18+
Example 2:
19+
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
20+
Output: 5
21+
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
22+
 
23+
24+
Note:
25+
26+
All of the nodes' values will be unique.
27+
p and q are different and both values will exist in the binary tree.
28+
29+
30+
31+
32+
33+
'''

Python/merge-intervals.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Given a collection of intervals, merge all overlapping intervals.
3+
4+
Example 1:
5+
6+
Input: [[1,3],[2,6],[8,10],[15,18]]
7+
Output: [[1,6],[8,10],[15,18]]
8+
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
9+
Example 2:
10+
11+
Input: [[1,4],[4,5]]
12+
Output: [[1,5]]
13+
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
14+
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
15+
16+
'''
17+
18+
19+
class Solution:
20+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
21+
intervals.sort(key=lambda x:(x[0],x[1]))
22+
res = []
23+
i = 0
24+
while i < len(intervals):
25+
j = i +1
26+
while j < len(intervals) and intervals[i][1] >= intervals[j][0]:
27+
intervals[i][1] = max(intervals[i][1], intervals[j][1])
28+
j += 1
29+
res.append(intervals[i])
30+
i = j
31+
return res

Python/multiply-strings.py

+12-22
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,24 @@
2424
class Solution:
2525
def multiply(self, num1: str, num2: str) -> str:
2626

27-
# Approach one 不符合题目要求
28-
# return str(int(num1) * int(num2))
29-
30-
31-
32-
# Approach two 采取列竖式乘法的方法, m位数乘以n位数,结果最多为m+n位数。
27+
# 考察大数相乘(两个数相乘结果只可能为n1+n2或者n1+n2-1)
3328
if num1 == "0" or num2 == "0": return "0"
3429
n1 = len(num1)
3530
n2 = len(num2)
31+
num1 = list(map(int, list(num1)))
32+
num2 = list(map(int, list(num2)))
3633
result = [0 for i in range(n1 + n2)]
3734

38-
def _sum(value, index): # 存储每一位相乘的结果
39-
tmpIndex = n1 + n2 - 1 - index
40-
tmp = result[tmpIndex] + value
41-
if tmp > 9:
42-
result[tmpIndex] = tmp % 10
43-
_sum(tmp // 10, index + 1)
44-
else:
45-
result[tmpIndex] = tmp
46-
47-
for i in range(n1):
48-
a = int(num1[n1 - 1 - i])
49-
for j in range(n2):
50-
b = int(num2[n2 - 1 - j])
51-
c = a * b
52-
_sum(c, i + j) # 从0开始
35+
for i in range(n1-1,-1,-1):
36+
for j in range(n2-1,-1,-1):
37+
result[i+j+1] += num1[i] * num2[j]
38+
39+
for i in range(n1+n2-1,0,-1):
40+
result[i-1] += result[i] // 10
41+
result[i] = result[i] % 10
42+
5343

5444
if result[0] == 0:
55-
return "".join([str(k) for k in result[1:]]) # 最多只有一位零
45+
return "".join([str(k) for k in result[1:]]) # 前面最多只有一位零
5646
else:
5747
return "".join([str(k) for k in result])

Python/subsets-ii.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
3+
4+
Note: The solution set must not contain duplicate subsets.
5+
6+
Example:
7+
8+
Input: [1,2,2]
9+
Output:
10+
[
11+
[2],
12+
[1],
13+
[1,2,2],
14+
[2,2],
15+
[1,2],
16+
[]
17+
]
18+
19+
20+
'''
21+
22+
23+
class Solution:
24+
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
25+
res = [[]]
26+
nums.sort()
27+
for i in range(len(nums)):
28+
if i == 0 or nums[i] != nums[i-1]:
29+
l = len(res)
30+
res += [ res[j] + [nums[i]] for j in range(len(res)-l, len(res))]
31+
return res

Python/unique-paths.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'''
2+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
3+
4+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
5+
6+
How many possible unique paths are there?
7+
8+
9+
Above is a 7 x 3 grid. How many possible unique paths are there?
10+
11+
Note: m and n will be at most 100.
12+
13+
Example 1:
14+
Input: m = 3, n = 2
15+
Output: 3
16+
Explanation:
17+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
18+
1. Right -> Right -> Down
19+
2. Right -> Down -> Right
20+
3. Down -> Right -> Right
21+
22+
23+
Example 2:
24+
Input: m = 7, n = 3
25+
Output: 28
26+
27+
28+
29+
'''
30+
31+
32+
33+
34+
class Solution:
35+
def uniquePaths(self, m: int, n: int) -> int:
36+
37+
38+
# Approach two 节省存储空间
39+
dp = [1 for _ in range(m)]
40+
for i in range(1,n):
41+
for j in range(1,m):
42+
dp[j] = dp[j-1] + dp[j]
43+
return dp[-1]
44+
45+
46+
47+
48+
# Approach one
49+
# dp = [[0 for _ in range(m)] for _ in range(n)]
50+
# dp[0] = [1 for _ in range(m)]
51+
# for i in range(n):
52+
# dp[i][0] = 1
53+
# for i in range(1,n):
54+
# for j in range(1,m):
55+
# dp[i][j] = dp[i-1][j] + dp[i][j-1]
56+
# return dp[n-1][m-1]

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ Binary Search Manipulation](https://github.com/OnlyChristmas/leetcode#bit-manipu
567567
## Sort
568568
| # | Title | Solution | Time | Space | Difficulty |
569569
| --- | --- | --- | --- | --- | --- |
570-
| 056 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/) | [Python](./Python/merge-intervals.py) | | | Hard |
570+
| 056 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/) | [Python](./Python/merge-intervals.py) | O(n) | O(1) | Medium |
571571
| 057 | [Insert Interval](https://leetcode.com/problems/insert-interval/) | [Python](./Python/insert-interval.py) | | | Hard |
572572
| 075 | [Sort Colors](https://leetcode.com/problems/sort-colors/) | [Python](./Python/sort-colors.py) | O(n) | O(1) | Medium |
573573
| 088 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Python](./Python/merge-sorted-array.py) | O(n) | O(1) | Easy |
@@ -795,9 +795,9 @@ Binary Search Manipulation](https://github.com/OnlyChristmas/leetcode#bit-manipu
795795
| 051 | [N-Queens](https://leetcode.com/problems/n-queens/) | [Python](./Python/n-queens.py) | | | Hard |
796796
| 052 | [N-Queens-II](https://leetcode.com/problems/n-queens-ii/) | [Python](./Python/n-queens-ii.py) | | | Hard |
797797
| 077 | [Combinations](https://leetcode.com/problems/combinations/) | [Python](./Python/combinations.py) | | | Medium |
798-
| 078 | [Subsets](https://leetcode.com/problems/subsets/) | [Python](./Python/subsets.py) | O(n2) | O(n) | Medium |
798+
| 078 | [Subsets](https://leetcode.com/problems/subsets/) | [Python](./Python/subsets.py) | O(n2) | O(1) | Medium |
799799
| 079 | [Word Search](https://leetcode.com/problems/word-search/) | [Python](./Python/word-search.py) | | | Medium |
800-
| 090 | [Subsets II](https://leetcode.com/problems/subsets-ii/) | [Python](./Python/subsets-ii.py) | | | Medium |
800+
| 090 | [Subsets II](https://leetcode.com/problems/subsets-ii/) | [Python](./Python/subsets-ii.py) | O(n^2) | O(1) | Medium |
801801
| 093 | [Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) | [Python](./Python/restore-ip-addresses.py) | | | Medium |
802802
| 126 | [Word Ladder II](https://leetcode.com/problems/word-ladder-ii/) | [Python](./Python/word-ladder-ii.py) | | | Hard |
803803
| 131 | [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) | [Python](./Python/palindrome-partitioning.py) | | | Medium |
@@ -824,7 +824,7 @@ Binary Search Manipulation](https://github.com/OnlyChristmas/leetcode#bit-manipu
824824
| --- | --- | --- | --- | --- | --- |
825825
| 010 | [Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/) | [Python](./Python/regular-expression-matching.py) | | | Hard |
826826
| 053 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [Python](./Python/maximum-subarray.py) | O(n) | O(1) | Easy |
827-
| 062 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | [Python](./Python/unique-paths.py) | | | Medium |
827+
| 062 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | [Python](./Python/unique-paths.py) | O(n*m) | O(n) | Medium |
828828
| 063 | [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) | [Python](./Python/unique-paths-ii.py) | | | Medium |
829829
| 064 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) | [Python](./Python/minimum-path-sum.py) | O(n^2) | O(1) | Medium |
830830
| 070 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Python](./Python/climbing-stairs.py) | O(n) | O(1) | Easy |

0 commit comments

Comments
 (0)