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. 第十章、collections

    目录 第十章.collections 一.OrderedDict方法 第十章.collections 一.OrderedDict方法 使用dict时,Key是无序的.在对dict做迭代时,我们无法确定 ...

  2. 用Buildout来构建Python项目

    用Buildout来构建Python项目   什么是Buildout (Remixed by Matt Hamilton, original from http://xkcd.com/303) Bui ...

  3. 利用协程和socket实现并发

    服务端代码 from gevent import monkey monkey.patch_all() from gevent import spawn import socket def commun ...

  4. ISO/IEC 15444-12 MP4 封装格式标准摘录 5

    目录 Segments Segment Type Box Segment Index Box Subsegment Index Box Producer Reference Time Box Supp ...

  5. pycharm 快捷键及一些常用设置

    pycharm中的快捷键及一些常用设置 在PyCharm /opt/pycharm-3.4.1/help目录下可以找到ReferenceCard.pdf快捷键英文版说明 PyCharm Default ...

  6. httpwatch

    https://blog.csdn.net/coderising/article/details/89530016 别人的文章

  7. Linux配置python环境1,pyenv

    安装pyenv sudo apt-get install curl git curl -L https://github.com/pyenv/pyenv-installer/raw/master/bi ...

  8. LOAD DATA INFILE & mysqlimport

    +++++++++++++++++++++++++++++++++++++++++++++mysqlimport++++++++++++++++++++++++++++++++++++++++++++ ...

  9. BBS-media配置

    media配置: 在上传头像的时候会用到media,首先需要在setting中加下面这一句话 MEDIA_ROOT=os.path.join(BASE_DIR,"blog",&qu ...

  10. 5 解析器、url路由控制、分页、渲染器和版本

    1 数据解析器 1 什么是解析器 相当于request 中content-type 对方传什么类型的数据,我接受什么样的数据:怎样解析 无论前面传的是什么数据,都可以解开 例如:django不能解析j ...