class Solution:
"""
@param nums: A list of integers.
@return: The maximum number inside the window at each moving.
"""
def maxSlidingWindow(self, nums, k):
# write your code here
ans = []
if len(nums) < k or not nums:
return ans
last_max_num = max(nums[0:k])
for i in range(0, len(nums)-k+1):
if nums[i+k-1] > last_max_num:
last_max_num = nums[i+k-1]
elif nums[i-1] < last_max_num:
pass
else:
last_max_num = max(nums[i:i+k])
ans.append(last_max_num)
return ans
LintCode:滑动窗口的最大值
最新推荐文章于 2018-08-11 16:07:15 发布