Skip to content

Commit a46c161

Browse files
committedAug 17, 2019
2019-08-16
1 parent 908534f commit a46c161

File tree

5 files changed

+37
-41
lines changed

5 files changed

+37
-41
lines changed
 

‎0236.二叉树的最近公共祖先/0236-二叉树的最近公共祖先.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@ def lowestCommonAncestor(self, root, p, q):
1313
:type q: TreeNode
1414
:rtype: TreeNode
1515
"""
16-
17-
if not root or root == p or root == q:
16+
if root in [p, q, None]:
1817
return root
1918
else:
2019
left = self.lowestCommonAncestor(root.left, p, q)
2120
right = self.lowestCommonAncestor(root.right, p, q)
2221

23-
if left and right: #一个在左子树,一个在右子树
22+
if left and right:
2423
return root
25-
elif left:#都在左子树
24+
elif left:
2625
return left
27-
elif right:#都在右子树
26+
elif right:
2827
return right
29-
else:
30-
return
31-
32-
28+

‎0238.除自身以外数组的乘积/0238-除自身以外数组的乘积.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ def productExceptSelf(self, nums):
44
:type nums: List[int]
55
:rtype: List[int]
66
"""
7-
l = len(nums)
8-
res = [1] * l
9-
for i in range(1,l):
10-
res[i] = res[i - 1] *nums[i - 1]
11-
12-
p = 1
13-
for i in reversed(range(l)):
14-
res[i] *= p
15-
p *= nums[i]
16-
17-
return res
18-
19-
7+
res = [1] * len(nums)
8+
for i in range(1, len(nums)):
9+
res[i] = res[i - 1] * nums[i - 1]
10+
tmp = 1
11+
for i in range(len(nums) - 1, -1, -1):
12+
res[i] *= tmp
13+
tmp *= nums[i]
14+
return res

‎0239.滑动窗口最大值/0239-滑动窗口最大值.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,17 @@ def maxSlidingWindow(self, nums, k):
77
"""
88
if not nums:
99
return []
10-
11-
window, res = list(), list()
12-
for i, x in enumerate(nums):
13-
14-
if window and window[0] <= i - k:
10+
11+
window = []
12+
res = []
13+
for i in range(len(nums)):
14+
if window and window[0] <= i - k: #当前window头应该被弹出
1515
window.pop(0)
16-
while window and nums[window[-1]] < x:
1716

17+
while window and nums[window[-1]] < nums[i]: #比 nums[i] 小的都不要,因为只要窗口的最大值
1818
window.pop()
1919

2020
window.append(i)
21-
2221
if i >= k - 1:
2322
res.append(nums[window[0]])
24-
25-
return res
26-
23+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def searchMatrix(self, matrix, target):
3+
"""
4+
:type matrix: List[List[int]]
5+
:type target: int
6+
:rtype: bool
7+
"""
8+
if not matrix or not matrix[0]:
9+
return False
10+
m, n = len(matrix), len(matrix[0])
11+
12+
for i in range(m):
13+
for j in range(n):
14+
if matrix[i][j] == target:
15+
return True
16+
return False

‎0242.有效的字母异位词/0242-有效的字母异位词.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,4 @@ def isAnagram(self, s, t):
55
:type t: str
66
:rtype: bool
77
"""
8-
hashmaps, hashmapt = dict(), dict()
9-
for char in s:
10-
hashmaps[char] = hashmaps.get(char, 0) + 1
11-
12-
for char in t:
13-
hashmapt[char] = hashmapt.get(char, 0) + 1
14-
15-
return hashmaps == hashmapt
16-
8+
return sorted(s) == sorted(t)

0 commit comments

Comments
 (0)
Please sign in to comment.