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(双指针)的更多相关文章

  1. 【刷题-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 ...

  2. [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 ...

  3. 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 ...

  4. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  5. LeetCode 209 Minimum Size Subarray Sum

    Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...

  6. 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 ...

  7. 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 ...

  8. 209. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. 【Leetcode】209. Minimum Size Subarray Sum

    Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...

随机推荐

  1. 利用Jenkins未授权获取服务器权限--Docker还来干扰--一次渗透的经历

    Jenkins获取权限的过程 Jenkins存在未授权访问漏洞 Jenkins存在未授权访问漏洞,且项目具有读取权限,通过项目的日志获取到一个账号密码,尝试登录成功,打开控制台成功. 备注:控制台一般 ...

  2. Python虚拟开发环境pipenv

    简介 requests库的作者,requests库的作者,requests库的作者(重要的事情说三遍)的又一力作,囊落了virtualenv, pip, pipfile等作为虚拟开发环境的命令行工具 ...

  3. 【BZOJ5133】[CodePlus2017年12月]白金元首与独舞 矩阵树定理

    [BZOJ5133][CodePlus2017年12月]白金元首与独舞 题面:www.lydsy.com/JudgeOnline/upload/201712/div1.pdf 题解:由于k很小,考虑用 ...

  4. Spring Cloud Eureka 配置

    实例名配置       在Netflix Eureka的原生实现中,实例名采用主机名作为默认值,这样的设置使得在同一主机上无法启动多个相同的实例,所以在Spring Cloud Eureka的配置中, ...

  5. [转][darkbaby]任天堂传——失落的泰坦王朝(中)

    TV游戏产业历史上曾有过太多表里不一的外交辞令,然而当年SQUARE和任天堂分道扬镳的真正原因确实如坂口博信在1996年2月29日的PS版 <FFVII>发表会上宣称的那样:“虽然之前有过 ...

  6. 你不可缺少的技能——Markdown编辑

    Markdown简介 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式.请不要被「标记」.「语言」所迷惑,Markdown 的语法十分 ...

  7. Mybatis批量insert报错的解决办法【the right syntax to use near '' at line...】

    Java中使用Mybatis批量插入数据时Mapper.xml中的sql如下: <insert id="batchSave"> into t_emp(emp_name, ...

  8. TensorFlow 度量张量和张量或者和零之间的误差值

    用于一个回归任务或者正则问题 # l2损失,output= sum(x ** 2)/2 inputdata = tf.Variable(np.random.rand(2,3), dtype=np.fl ...

  9. traceroute 排查 nginx 反向代理 配置

    [root@b ~]# traceroute www.test.comtraceroute to www.test.com (134.175.58.66), 30 hops max, 60 byte ...

  10. UNSIGNED command-line client

    High Performance MySQL, Third Edition by Baron Schwartz, Peter Zaitsev, and Vadim Tkachenko   There ...