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 ...
随机推荐
- Thinkphp5.0 的使用模型Model的获取器与修改器
Thinkphp5.0 的使用模型Model的获取器.修改器.软删除 一.获取器 在model中使用 get+字段名+Attr,可以修改字段的返回值. 数据库中性别保存为,0未知.1男.2女,查询时返 ...
- 从零开始写STL—栈和队列
从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...
- POJ 3268_Silver Cow Party
题意: n个地方,标号1~n,每个地方都有一头牛,现在要他们都去往标号为x的地方,再从x返回,每条道路都是单向的,求所有牛走的来回的最短路中的最大值. 分析: 注意在求每头牛走到x时,挨个算肯定超时, ...
- JSTL-XML标签库
主页:http://www.cnblogs.com/EasonJim/p/6958992.html的分支页. 一.<x:out> <x:out>标签显示XPath表达式的结果, ...
- dtrace-conf 2016
https://www.joyent.com/about/events/2016/dtrace-conf
- 关于SQL命令中不等号(!=,<>)
比较两个表达式(比较运算符).当比较非空表达式时,如果左边操作数的数值不等于右边的操作数,则结果为 TRUE:否则结果为 FALSE.如果两个操作数中有一个或者两个都为 NULL,并且 SET ANS ...
- CSS聊天气泡
概述 谷歌效果图如下: ie效果图如下: 完整代码 <!DOCTYPE html> <html> <head> <meta charset="gbk ...
- 【c++】【转】如何只在heap上创建对象,如何只在stack上建立对象?
http://www.cnblogs.com/chio/archive/2007/10/23/934335.html http://blog.csdn.net/szchtx/article/detai ...
- 文件权限的获取,cmd命令:Takeown
takeown /f * /a /r /d y #强制将当前目录下的所有文件及文件夹.子文件夹下的所有者更改为管理员组(administrators)命令: cacls d:documents*.* ...
- MAVEN项目模块化
maven的最大的特点之中的一个就是能够把项目模块化. 前面的一篇文章MAVEN创建并打包web项目已经创建了一个简单的webapp,注意这个webapp的打包方式是war. 假设如今又要划分出来一个 ...