题解 | #滑动窗口的最大值#
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param num int整型一维数组
# @param size int整型
# @return int整型一维数组
#
class Solution:
def maxInWindows(self , num: List[int], size: int) -> List[int]:
# write code here
#计算出滑动窗口的数组
#比较数组中每个数组的最大值
#result存放滑动窗口
#然后再遍历每个窗口,求一个最大值
if not size:
return []
if len(num)<size:
return []
L=[]
L2=[]
for i in range(len(num)):
if i+size<= len(num):
L.append(num[i:i+size])
else:
break
for x in L:
L2.append(max(x))
return L2
查看6道真题和解析