209. Minimum Size Subarray Sum(双指针)
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
Input:[2,3,1,2,4,3],s = 7
Output: 2
Explanation: the subarray[4,3]has the minimal length under the problem constraint.
s = , nums = [,,,,,] ^
l
r
上边的窗口内所有数字的和 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + + 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + + + 大于等于了 , 记录此时的长度 min = , l 右移 ^ ^
l r
上边的窗口内所有数字的和 + + 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + + + 大于等于了 , 更新此时的长度 min = , l 右移 ^ ^
l r
上边的窗口内所有数字的和 + + 大于等于了 , 更新此时的长度 min = , l 右移 ^ ^
l r
上边的窗口内所有数字的和 + 小于 , r 右移 ^ ^
l r
上边的窗口内所有数字的和 + + 大于等于了 , 更新此时的长度 min = , l 右移 ^ ^
l r
上边的窗口内所有数字的和 + 大于等于了 , 更新此时的长度 min = , l 右移 ^
r
l
上边的窗口内所有数字的和 小于 , r 右移,结束
class Solution {
public:
int minSubArrayLen(int s, vector<int>& a) {
int slow = 0;
int min_res = INT_MAX;
int n = a.size();
int sum ;
for (int i = 0; i < n; i++) {
sum += a[i];
while(sum >= s) {
min_res = std::min(min_res,i+1-slow);
sum-=a[slow++];
}
}
return (min_res != INT_MAX) ? min_res : 0;
}
};
class Solution {
public int minSubArrayLen(int target, int[] a) {
if(a.length==0||a.length==1)
return 0;
int i = 0,j = 0,sum =0 ,min = Integer.MAX_VALUE;
while(j<a.length){
sum+=a[j++];
while(sum>=target){
min = Math.min(min,j-i);
sum-=a[i++];
}
}
return min==Integer.MAX_VALUE?0:min;
}
}
209. Minimum Size Subarray Sum(双指针)的更多相关文章
- 【刷题-LeetCode】209. Minimum Size Subarray Sum
Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...
- [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- LeetCode OJ 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- LeetCode 209. Minimum Size Subarray Sum (最短子数组之和)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- Java for LeetCode 209 Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【Leetcode】209. Minimum Size Subarray Sum
Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...
随机推荐
- CMOS构成的常见电路
CMOS门电路 以MOS(Metal-Oxide Semiconductor)管作为开关元件的门电路称为MOS门电路.由于MOS型集成门电路具有制造工艺简单.集成度高.功耗小以及抗干扰能力强等优点,因 ...
- 应用Strong Name保存.NET应用程序集
关于Strong Name的主题,网上已经有很多这方面的介绍,你可能最熟悉的印象就是这样 大部分的情况,这样就可以了.如果代码是机密的,还可能用到Delay sign only,这就复杂一些,请查找相 ...
- linux shell中curl 发送post请求json格式问题
今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ...
- 利用bat批处理做启动mongodb脚本
文章开始,我们先回顾一下,如何用cmd命令窗口开启mongodb数据库,命令如下: 开启mongodb数据库 cd D:\Program Files\MongoDB\bin mongod --depa ...
- iOS - 获取音视频文件的Metadata信息
// // MusicInfoArray.h // LocationMusic // // Created by Wengrp on 2017/6/22. // Copyright © 2017年 W ...
- inline-blcok 之间的空白间隙
前言: inline-blcok 布局时,通常情况下, inline-blocks 之间有空白,尽管通常我们是不想要的,毕竟不像padding或者margin一样好控制,如图: <div cla ...
- CentOS环境下jdk安装部署
1.准备jdk安装文件: 这里我使用的是 jdk-7u79-linux-x64.tar.gz 2.在 /usr/local 目录下创建 sotfware目录,并上传JDK文件: 解压文件并修改文件夹为 ...
- POJ-2081 Terrible Sets(暴力,单调栈)
Terrible Sets Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4113 Accepted: 2122 Descrip ...
- Java 中运用DOS运行class(字节码)
附属: -dir:例举该目录的所有文件名称 有<dir>是文件夹,没有<dir>是文件-cd: 改变目录 进入其他目录 change direction-cd\:一次性回到根目 ...
- MySQL复制原理
mysql从3.23开始提供复制功能,复制指将主库的ddl和dml操作通过binlog文件传送到从库上执行,从而保持主库和从库数据同步.mysql支持一台主库同时向多台从库复制,从库同时也可以作为其他 ...