Skip to content

Commit

Permalink
2019-08-16
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Aug 17, 2019
1 parent 908534f commit a46c161
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ def lowestCommonAncestor(self, root, p, q):
:type q: TreeNode
:rtype: TreeNode
"""

if not root or root == p or root == q:
if root in [p, q, None]:
return root
else:
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)

if left and right: #一个在左子树,一个在右子树
if left and right:
return root
elif left:#都在左子树
elif left:
return left
elif right:#都在右子树
elif right:
return right
else:
return



Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ def productExceptSelf(self, nums):
:type nums: List[int]
:rtype: List[int]
"""
l = len(nums)
res = [1] * l
for i in range(1,l):
res[i] = res[i - 1] *nums[i - 1]

p = 1
for i in reversed(range(l)):
res[i] *= p
p *= nums[i]

return res


res = [1] * len(nums)
for i in range(1, len(nums)):
res[i] = res[i - 1] * nums[i - 1]
tmp = 1
for i in range(len(nums) - 1, -1, -1):
res[i] *= tmp
tmp *= nums[i]
return res
17 changes: 7 additions & 10 deletions 0239.滑动窗口最大值/0239-滑动窗口最大值.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ def maxSlidingWindow(self, nums, k):
"""
if not nums:
return []
window, res = list(), list()
for i, x in enumerate(nums):
if window and window[0] <= i - k:

window = []
res = []
for i in range(len(nums)):
if window and window[0] <= i - k: #当前window头应该被弹出
window.pop(0)
while window and nums[window[-1]] < x:

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

window.append(i)

if i >= k - 1:
res.append(nums[window[0]])

return res

return res
16 changes: 16 additions & 0 deletions 0240.搜索二维矩阵II/0240-搜索二维矩阵II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix or not matrix[0]:
return False
m, n = len(matrix), len(matrix[0])

for i in range(m):
for j in range(n):
if matrix[i][j] == target:
return True
return False
10 changes: 1 addition & 9 deletions 0242.有效的字母异位词/0242-有效的字母异位词.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,4 @@ def isAnagram(self, s, t):
:type t: str
:rtype: bool
"""
hashmaps, hashmapt = dict(), dict()
for char in s:
hashmaps[char] = hashmaps.get(char, 0) + 1

for char in t:
hashmapt[char] = hashmapt.get(char, 0) + 1

return hashmaps == hashmapt

return sorted(s) == sorted(t)

0 comments on commit a46c161

Please sign in to comment.