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 ...
随机推荐
- JVM(二):Java中的语法糖
JVM(二):Java中的语法糖 上文讲到在语义分析中会对Java中的语法糖进行解糖操作,因此本文就主要讲述一下Java中有哪些语法糖,每个语法糖在解糖过后的原始代码,以及这些语法糖背后的逻辑. 语法 ...
- MYSQL 时间数据类型
- html上传图片类型
<html> <head> <meta charset="utf-8"> <title>上传图片</title> ...
- Atitit.auto complete 自己主动完毕控件的实现总结
Atitit.auto complete 自己主动完毕控件的实现总结 1. 框架选型 1 2. 自己主动完毕控件的ioc设置 1 3. Liger 自己主动完毕控件问题 1 4. 官网上的code ...
- ATM取款机模拟——数据结构课设
今天帮人写的第二篇课设 . ;-) 机智的窝 要求:大概说一下吧,就是要创建一个用户(初始化一账户),模拟ATM的业务(取款,100的整数倍,改密 码,查剩余金额.等等,各 ...
- 用Java开发50个棋类游戏
眼下已经开发完了两个 1A2B 24点 打算开发以下的.直接在QQ上玩. QQ机器人已经有了.我们直接写业务即可.有兴趣的參与.机器人婷婷体验群 Java技术交流 207224939 四棋 小枪大炮 ...
- Nginx中配置vue,react项目地址
如题 像以前在Nginx中配置域名解析的时候只需要在conf.d文件夹下添加对应的xx.conf文件(当然了你也可以在nginx.conf)下配置. 如果是以前的老项目只需要在配置文件中server内 ...
- web 开发之js---js获取select标签选中的值
var obj = document.getElementByIdx_x(”testSelect”); //定位id var index = obj.selectedIndex; // 选中索引 va ...
- dumpdecrypted进行砸壳
1.下载源码git clone git://github.com/stefanesser/dumpdecrypted/ 2.进行编译生成 dumpdecrypted.dylibmake 3.将dump ...
- nginxserver报403 forbidden错误的解决的方法
改动nginx.config文件内容: location / { #root html; root D:\java; ...