面试题65:滑动窗口的最大值
2019-05-05 本文已影响0人
gpfworld
题目
Code
# -*- coding:utf-8 -*-
from collections import deque
class Solution:
def maxInWindows(self, num, size):
# write code here
maxinNum = []
if len(num) >= size and size >= 1:
index = deque()
for i in range(size):
while index and num[i] > num[index[-1]]:
index.pop()
index.append(i)
for i in range(size,len(num)):
maxinNum.append(num[index[0]])
while index and num[i] > num[index[-1]]:
index.pop()
if len(index)!=0 and index[0] <= (i -size):
index.popleft()
index.append(i)
maxinNum.append(num[index[0]])
return maxinNum
注意:
index 对象判空的用法
while index and num[i] > num[index[-1]]: