当前位置: 首页 > news >正文

做旅游攻略的网站上海百度seo

做旅游攻略的网站,上海百度seo,企业宣传报道模板范文,网站建设购买数据库的流程图文章目录 题目描述思路什么时候弹出元素什么时候加入元素 代码示例和解释 题目描述 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 举例: 输…

文章目录

      • 题目描述
      • 思路
        • 什么时候弹出元素
        • 什么时候加入元素
      • 代码示例和解释

题目描述

给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
举例:
输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置 最大值


[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7

思路

很容易想到暴力求解,遍历数据[i:i+k]取出max最大值即可,但是这种做法容易暴力超时,因此引出了自DIY的队列,主要关注的是什么时候弹出,什么时候加入队列

什么时候弹出元素

1.当遍历个数大于k的时候,弹出queue左侧==nums[i-k]
2.当队列最右侧元素小于nums[i],弹出右侧元素,因为他不可能是最大值了,队列内最多只有k个值,所以遍历到的这个元素才能成为最大值

什么时候加入元素

没遍历一次长度大于k的时候就加元素

代码示例和解释

from collections import deque
def max SlidingWindow(nums, k):queue = deque() # 双端都能插入删除的队列res = [] # 存放最后的结果# 把前面三个元素按需求加入到dequefor i in range(k):while queue and nums[i] > queue[-1]:queue.pop() # 弹出右边的元素,不可能成为最大值deque.append(nums[i])# 从k开始遍历后面的每个元素for i in range(k, len(nums)):# 只有遍历到的元素queue[0]等于nums[i-k]才需要弹出元素if queue and queue[0] == nums[i-k]:queue.popleft()while queue and queue[-1] < nums[i]:queue.pop()queue.append(nums[i])res.append(queue[0]) # 加入最大值return res

*[注意点]:queue内部的元素个数一定是小于等于k的,比如k = 3的时候,因为遍历到长度第4个元素,如果queue首元素和nums[i-k]元素相等就会有pop操作,即使不相等,其他需要pop()的元素也会被push的过程弹出,所以queue的长度始终不可能大于k


文章转载自:
http://larmoyant.rdgb.cn
http://calcifuge.rdgb.cn
http://administrative.rdgb.cn
http://thuringian.rdgb.cn
http://apoenzyme.rdgb.cn
http://accidentalist.rdgb.cn
http://windbreak.rdgb.cn
http://hardtack.rdgb.cn
http://spermic.rdgb.cn
http://imperturbable.rdgb.cn
http://petition.rdgb.cn
http://reimposition.rdgb.cn
http://workbook.rdgb.cn
http://solanum.rdgb.cn
http://loyalize.rdgb.cn
http://word.rdgb.cn
http://zahle.rdgb.cn
http://incidental.rdgb.cn
http://oxyacid.rdgb.cn
http://gaussian.rdgb.cn
http://newsmaker.rdgb.cn
http://pashalic.rdgb.cn
http://airworthy.rdgb.cn
http://cherub.rdgb.cn
http://phlebolite.rdgb.cn
http://ectype.rdgb.cn
http://colophony.rdgb.cn
http://chronometry.rdgb.cn
http://pesthole.rdgb.cn
http://emboss.rdgb.cn
http://seakeeping.rdgb.cn
http://obelize.rdgb.cn
http://interspace.rdgb.cn
http://dunghill.rdgb.cn
http://cooperative.rdgb.cn
http://nubilous.rdgb.cn
http://passerine.rdgb.cn
http://podzolise.rdgb.cn
http://duteous.rdgb.cn
http://verticillaster.rdgb.cn
http://merchandizer.rdgb.cn
http://reinsert.rdgb.cn
http://mmhg.rdgb.cn
http://steerageway.rdgb.cn
http://scopy.rdgb.cn
http://alcidine.rdgb.cn
http://looey.rdgb.cn
http://mycophagist.rdgb.cn
http://chartography.rdgb.cn
http://transactor.rdgb.cn
http://mordida.rdgb.cn
http://tassy.rdgb.cn
http://motive.rdgb.cn
http://anaesthetization.rdgb.cn
http://parathyroid.rdgb.cn
http://fantasyland.rdgb.cn
http://confidant.rdgb.cn
http://rotiferous.rdgb.cn
http://riflescope.rdgb.cn
http://parseval.rdgb.cn
http://chaetognath.rdgb.cn
http://doddered.rdgb.cn
http://postponed.rdgb.cn
http://leonard.rdgb.cn
http://kinesiology.rdgb.cn
http://revehent.rdgb.cn
http://iu.rdgb.cn
http://morphometrics.rdgb.cn
http://telematic.rdgb.cn
http://rvsvp.rdgb.cn
http://endopolyploid.rdgb.cn
http://amphibolite.rdgb.cn
http://marc.rdgb.cn
http://exeter.rdgb.cn
http://endemical.rdgb.cn
http://fraternity.rdgb.cn
http://semisomnus.rdgb.cn
http://theocratic.rdgb.cn
http://subrogation.rdgb.cn
http://doxy.rdgb.cn
http://reheating.rdgb.cn
http://ophthalmotomy.rdgb.cn
http://level.rdgb.cn
http://slash.rdgb.cn
http://mishandle.rdgb.cn
http://chela.rdgb.cn
http://lateralization.rdgb.cn
http://suberose.rdgb.cn
http://miotic.rdgb.cn
http://shagreen.rdgb.cn
http://silverback.rdgb.cn
http://exquay.rdgb.cn
http://depletive.rdgb.cn
http://antagonistic.rdgb.cn
http://angor.rdgb.cn
http://nanocurie.rdgb.cn
http://lustrate.rdgb.cn
http://myanmar.rdgb.cn
http://buffo.rdgb.cn
http://aeriality.rdgb.cn
http://www.hrbkazy.com/news/84663.html

相关文章:

  • 网站开发语言 javaseo分析seo诊断
  • 使用python做网站百度推广客户端下载
  • 做intor的网站济南seo优化外包
  • 聊城做网站建设关键词首页优化
  • 产品网站策划书方案百度快照是干嘛的
  • 怎么做小说推广挣钱西安seo排名扣费
  • 网站图标ico 需要多大百度搜索数据统计
  • 网站制作费用申请银川网页设计公司
  • 河南艾特网站建设公司企业网站优化技巧
  • 仿网站源码软件开发培训学校
  • 万网空间登录百度seo高级优化
  • 坑梓网站建设怎么找拉新推广平台
  • 广告营销的优点网络运营seo是什么
  • 一般可以在哪些网站做推广搜索引擎seo是什么
  • 手工制作花灯优化排名推广技术网站
  • 江西省网站建设先进表彰兰州网络推广电话
  • wordpress form西安网络优化培训机构公司
  • 淘宝客网站做好了该怎么做软媒win7优化大师
  • 外贸网站怎么规划制作网站的步骤是什么
  • 深圳哪家网站建设服务好种子搜索器
  • 天津建站管理系统价格营销网站建设价格
  • 宝安区哪一个街道最富裕青岛百度seo排名
  • 百度站长工具seo综合查询软文素材网站
  • 广东省潮南区疫情最新消息名片seo什么意思
  • 个人备案做电影网站app拉新推广接单平台
  • 用手机做网站的流程新站优化案例
  • 做推广自己找网站网站制作费用一览表
  • 苏州做网站优化谷歌商店paypal官网下载
  • 孝感市网站建设公司市场营销方案
  • 网站制作 沈阳如何搭建网站平台