mycode  89.27%

class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
""" if k == 0 or nums == [] or k > len(nums): return [] length = len(nums)
temp = []
for j in range(0,k):
temp.append(nums[j])
first = max(temp)
flag = first == nums[0]
res = [first]
for i in range(1,length-k+1):
if not flag :
first = max(first,nums[i+k-1])
else:
temp = []
for j in range(0,k):
temp.append(nums[i+j])
first = max(temp)
res.append(first)
flag = first == nums[i]
return res

参考

class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
if not nums: return [] # max for the first window
cur_max = max(nums[:k])
res = [cur_max]
left = 0 for right in range(k, len(nums)):
# recalculate the new max and index as the previous max is not in this window
if nums[right] > cur_max or nums[right-k] == cur_max :
cur_max = max(nums[right-k+1: right+1]) res.append(cur_max) return res

leetcode-hard-array-239. Sliding Window Maximum的更多相关文章

  1. 【LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum   Given an array nums, there is a sliding window of size k which is moving fr ...

  2. 【刷题-LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...

  3. [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  4. 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...

  5. [leetcode]239. Sliding Window Maximum滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  6. 239. Sliding Window Maximum

    题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...

  7. 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  8. (heap)239. Sliding Window Maximum

    题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...

  9. leetcode 239 Sliding Window Maximum

    这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...

  10. [leetcode] #239 Sliding Window Maximum (Hard)

    原题链接 题意: 给定一个数组数字,有一个大小为k的滑动窗口,它从数组的最左边移动到最右边.你只能在窗口看到k个数字.每次滑动窗口向右移动一个位置. 记录每一次窗口内的最大值,返回记录的值. 思路: ...

随机推荐

  1. Golang新开发者要注意的陷阱和常见错误

    转自:http://colobu.com/2015/09/07/gotchas-and-common-mistakes-in-go-golang/ 目录 [−] 初级 开大括号不能放在单独的一行 未使 ...

  2. HBuilderX 打包

    新建 - 云打包 (密钥 密码看不到 - 回车)    (  ) BlueStacks蓝叠 模拟器看效果

  3. Solr集群的搭建概述(非教程)

    1.什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候是不 ...

  4. MySQL导出数据到文件中

    一.导出一张表数据 把test_time表中的数据导出成txt 文件 mysql> show global variables like '%secure%'; +--------------- ...

  5. 根文件系统ramdisk.image.gz && uramdisk.image.gz

    1. 根文件系统镜像ramdisk.image.gz和uramdisk.image.gz 通常需要将文件系统输入到Nand Flash当中时,一般可以将根文件系统打包成uramdisk.image.g ...

  6. 九,configMap及secret的基本使用

    目录 制定容器配置的方式 configMap(存储数据为明文,敏感数据慎用) 创建configMap的几种方式 命令行创建和测试configMap实例 创建一个Pod 挂载测试 通过指定文件创建con ...

  7. RHEL7中配置本地YUM软件源

     1.创建目录,挂载光盘 [root@localhost ~]# mkdir /mnt/iso [root@localhost ~]# mount /dev/sr0  /mnt/iso mount: ...

  8. 内置函数 lambda sorted filter map 递归

    一 lambda 匿名函数 为了解决一些简单的需求而设计的一句话函数 # 计算 n 的 n次方 def func(n): return n**n print(func(10)) f = lambda ...

  9. Python 编解码

    字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...

  10. Django REST Framework(DRF)_第四篇

    DRF分页(总共三种) PageNumberPagination(指定第n页,每页显示n条数据) 说明 既然要用人家的那么我们就先来看下源码,这个分页类源码中举例通过参数指定第几页和每页显示的数据:h ...