mirror of
https://github.com/SunnyQjm/algorithm-review.git
synced 2026-06-05 15:39:29 +08:00
update
This commit is contained in:
@@ -31,7 +31,7 @@
|
||||
#######################################################################################
|
||||
|
||||
import collections
|
||||
|
||||
|
||||
|
||||
class Solution:
|
||||
def maxSlidingWindow(self, nums, k):
|
||||
@@ -105,9 +105,28 @@ class Solution:
|
||||
|
||||
return res
|
||||
|
||||
def maxSlidingWindow2(self, nums, k):
|
||||
if not nums:
|
||||
return []
|
||||
window, res = [], []
|
||||
|
||||
for i, x in enumerate(nums):
|
||||
if i >= k and window[0] <= i - k:
|
||||
window.pop(0)
|
||||
while window and nums[window[-1]] <= x:
|
||||
window.pop()
|
||||
window.append(i)
|
||||
if i >= k - 1:
|
||||
res.append(nums[window[0]])
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
solution = Solution()
|
||||
print(solution.maxSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3), "= \n[3, 3, 5, 5, 6, 7]")
|
||||
print(solution.maxSlidingWindow([1, 3, 1, 2, 0, 5], 3), "= \n[3, 3, 2, 5]")
|
||||
print(solution.maxSlidingWindow([9, 10, 9, -7, -4, -8, 2, -6], 5), "= \n[10, 10, 9, 2]")
|
||||
|
||||
print(solution.maxSlidingWindow2([1, 3, -1, -3, 5, 3, 6, 7], 3), "= \n[3, 3, 5, 5, 6, 7]")
|
||||
print(solution.maxSlidingWindow2([1, 3, 1, 2, 0, 5], 3), "= \n[3, 3, 2, 5]")
|
||||
print(solution.maxSlidingWindow2([9, 10, 9, -7, -4, -8, 2, -6], 5), "= \n[10, 10, 9, 2]")
|
||||
|
||||
@@ -35,7 +35,6 @@ class Solution:
|
||||
a. 用一个字典记录已经访问过的值及其下标
|
||||
b. 如果访问到某个值num[i]是,发现其匹配值target - nums[i] 出现在dict里面,表示其左边有一个是可以与nums[i]想加得target,
|
||||
此时返回[dict[nums[i]], i]即为结果
|
||||
4. 过程中使用set对结果集去重,保证没有重复的三元组
|
||||
"""
|
||||
|
||||
# 特判,过滤掉数组元素不够三个的情况
|
||||
|
||||
@@ -19,7 +19,7 @@ class ListNode:
|
||||
def __init__(self, x):
|
||||
self.val = x
|
||||
self.next = None
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
if self:
|
||||
return "{}->{}".format(self.val, repr(self.next))
|
||||
@@ -42,6 +42,7 @@ class Solution:
|
||||
head.next, cur, head = cur, head, head.next
|
||||
return cur
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
head = ListNode(1)
|
||||
head.next = ListNode(2)
|
||||
|
||||
Reference in New Issue
Block a user