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 ...
随机推荐
- MySQL对数据表进行分组查询(GROUP BY)
MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...
- ms转成00:00:00的时间格式化
毫秒转成 00:00:00的时间格式 比如1000毫秒转成00:00:01 /** * 格式化邀请的时间 * @param time ms */ public static formatTime(ti ...
- Android sd卡log日志
import android.os.Environment; import android.util.Log; import java.io.File; import java.io.FileOutp ...
- python的类中为什么要引入self
从第一次接触python的面向对象编程时起就很看不惯它的self,简直反人类. 相关资源: What is the purpose of self in Python? http://stackove ...
- Windows Server 2008 R2之三管理活动目录数据库
活动目录数据库包括数据库文件NTDS.dit和日志文件.考虑到最佳性能,在生产环境推荐将日志文件和数据库文件在单独的硬盘驱动器中或RAID中,同时要根据网络的规模,保证磁盘上有充足的剩余空间.由于活动 ...
- 微信都在用的移动敏捷测试方法和工具|视频+PPT
本文是腾讯优测总监雷彬在MPD2016 北京站上的演讲视频.他详细讲述了腾讯多年来在实践敏捷研发过程中测试的优化之路,为测试角色(包括测试工程师和开发自测)提供敏捷作业的思路.点击此处观看视频.时长5 ...
- POJ 1700 - Crossing River
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13982 Accepted: 5349 Description A gr ...
- Taking a peek inside with the Actuator
Taking a peek inside with the Actuator <dependency> <groupId>org.springframework.boot< ...
- arcgis server multipoint 服务 applyedit功能
首先打开arcmap,在catalog中新建 File GDB 在File GDB 中新建 Feature Class 类型选择 Multipoint 选择坐标 此时图层列表中有了新建的要素,点击Ed ...
- Ancient Go---hdu5546(dfs爆搜CCPC题目)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 题意就是两个人下围棋,问在下一颗x是否能杀死o,'.'是空位子: 枚举所有的点,判断是否合法即可 ...