239 Sliding Window Maximum 滑动窗口最大值
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。
例如,
给定 nums = [1,3,-1,-3,5,3,6,7],和 k = 3 。
窗口位置 最大值
--------------- -----
[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
由此可见,返回最大的滑动窗口为:[3,3,5,5,6,7]。
注意:
你可以假设 k 一直都是有效的,例如:1 ≤ k ≤ 输入数组的大小,输入数组不为空。
进阶:
你能在线性时间复杂度内解决此题吗?
详见:https://leetcode.com/problems/sliding-window-maximum/description/
Java实现:
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n=nums.length;
if(n==0||nums==null){
return new int[0];
}
int[] res=new int[n-k+1];
for(int i=0;i<n-k+1;++i){
int maxVal=nums[i];
for(int j=i;j<i+k;++j){
if(nums[j]>maxVal){
maxVal=nums[j];
}
}
res[i]=maxVal;
}
return res;
}
}
C++实现:
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n=nums.size();
vector<int> res;
if(n==0||nums.empty())
{
return res;
}
for(int i=0;i<n-k+1;++i)
{
int maxVal=nums[i];
for(int j=i;j<i+k;++j)
{
if(nums[j]>maxVal)
{
maxVal=nums[j];
}
}
res.push_back(maxVal);
}
return res;
}
};
239 Sliding Window Maximum 滑动窗口最大值的更多相关文章
- [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 ...
- [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 ...
- [LeetCode] 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 ...
- [LeetCode] Sliding Window Median 滑动窗口中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
- 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 ...
- POJ - 2823 Sliding Window (滑动窗口入门)
An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from t ...
- Sliding Window(滑动窗口)
Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 58002 Accepted: 16616 Case Time Limi ...
随机推荐
- Uva -1515 Pool construction(最小割)
输入一个字符矩阵,'.'代表洞,'#'代表草地.可以把草改成洞花费为d,或者把洞改成草花费为f,最后还要在草和洞之间修围栏花费为b. 首先把最外一圈的洞变成草,并累加花费. 增加一个源点和一个汇点,源 ...
- [bzoj2453]维护队列_带修改莫队
维护队列 bzoj-2453 题目大意:给定一个n个数序列,支持查询区间数的种类数,单点修改.不强制在线. 注释:$1\le n,m\le 10^5$. 想法: 带修改莫队裸题. 如果没有修改操作的话 ...
- 洛谷——P1151 子数整数
P1151 子数整数 题目描述 对于一个五位数a1a2a3a4a5,可将其拆分为三个子数: sub1=a1a2a3 sub2=a2a3a4 sub3=a3a4a5 例如,五位数20207可以拆分成 s ...
- Ubuntu 16.04安装JMeter测试工具
JMeter是Java的测试工具,由Apache开发. 同样,JMeter是跨平台的. 下载: http://jmeter.apache.org/download_jmeter.cgi 安装: 7z ...
- MongoDB C#驱动
烟波钓徒 MongoDB C#驱动 http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial 笔记 首先下载驱动.驱动有两个文件 Mongo ...
- swift container server 莫名stuck
openstack swift container server的进程经常莫名其妙进入 D Ds等状态 记录一下这个时候 storage.error的log 便于分析 一种情形是下面这种log Jun ...
- tornado的http服务器实现
使用tornado实现的一个简单http服务器:只需要定义自己的处理方法,其他的东西全部交给tornado完成. #coding:utf-8 import tornado.httpserver imp ...
- Elasticsearch学习系列之单模式下API的增删改查操作
这里我们通过Elasticsearch的marvel插件实现单模式下API的增删改查操作 索引的初始化操作 创建索引之前可以对索引进行初始化操作,比如先指定shard数量以及replicas的数量 代 ...
- Telnet登入cisco router 1800
Login to Router and change to privileged modec:\>telnet 192.168.6.1Trying 192.168.6.1...Connected ...
- Cookie对象的特点
1.存储少量不重要的数据2.存储在客户端的文本文件中(必须设置有效期,否则不被存储)3.安全性差4.存储的数据类型--字符串5.浏览器窗口无关,但与访问的站点相关6.具体特定的过期时间和日期7.在客户 ...