【Leetcode】209. Minimum Size Subarray Sum
Question:
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.
For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.
Tips:
给定一个长度为n的正整数数组,以及一个正整数s。找到长度最小的连续子数组,使他们的和大于等于s,并返回子数组的长度。如果不存在这样的子数组,就返回0.
思路:
设置两个变量,slow记录子数组的第一个数字位置,fast记录子数组当前要加到sum的数字位置。
fast从0位置开始向后移动,每次都加入到sum中sum+=nums[fast++];
当sum>=s时,最终结果ans就取原来的ans与fast-slow+1之间的最小值。在对sum进行减操作,减掉nums[slow],并继续判断sum是否仍然大于等于s。如果扔>=,slow向前移动,继续减nums[slow],
否则再继续在sum上加nums[fast]。
代码:
public int minSubArrayLen(int s, int[] nums) {
if(nums==null || nums.length<=0) return 0;
int len=nums.length;
int ans=Integer.MAX_VALUE;
int slow=0,fast=0;
int sum=0;
while(fast<len){
sum+=nums[fast++];
while(sum>=s){
//前面sum与nums[fast++]相加,fast也自加了1,所以比较min与fast-slow即可
ans=Math.min(fast-slow,ans);
sum-=nums[slow++];
if(sum==0)return 1;//代表刚减掉的nums[slow]=s
}
}
return ans==Integer.MAX_VALUE?0:ans;
}
【Leetcode】209. Minimum Size Subarray Sum的更多相关文章
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- 【刷题-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 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】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- [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 ...
- 209. Minimum Size Subarray Sum(双指针)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- 209. Minimum Size Subarray Sum【滑动窗口】
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
随机推荐
- div宽度随屏幕大小变化
题目: 一个页面上两个div左右铺满整个浏览器, 要保证左边的div一直为100px,右边的div跟随浏览器大小变化, 比如浏览器为500,右边div为400,浏览器为900,右边div为800. 方 ...
- docker swarm英文文档学习-2-关键概念
参考https://docs.docker.com/engine/swarm/key-concepts/ Swarm mode key concepts集群模式关键概念 本主题介绍Docker Eng ...
- OpenCV——边缘检测入门、Canny边缘检测
边缘检测的一般步骤: 最优边缘检测的三个评价标准: 低错误率:表示出尽可能多的实际边缘,同时尽可能地减少噪声产生的误报: 高定位性:标识出的边缘要与图像实际边缘尽可能接近: 最小响应:图像中的边缘只能 ...
- JAVA框架Struts2--配置讲解
一.配置讲解: <struts> <package name="oop" namespace="/" extends="struts ...
- 编写陈旭,实现通过字符型变量创建boolean值,再将其转换为字符串输出,观察输出后的字符串与创建Boolean对象时给定的参数是否相等.
主要涉及到基本数据类型与包装类的转换. Boolean类的tostring方法的作用,返回一个表示该布尔值的 String 对象 public class Test1 { public static ...
- Android 关于Activity的四种启动模式的简单介绍
Activity启动模式设置: <activity android:name=".MainActivity" android:launchMode="standar ...
- php中经常使用的string函数
strpos() ---返回字符串在另一字符串中首次出现的位置 strrpos() ---查找字符串在另一字符串中最后出现的位置 strchr() === strstr() ---找到字符 ...
- cf244D. Match & Catch 字符串hash (模板)或 后缀数组。。。
D. Match & Catch 能够用各种方法做.字符串hash.后缀数组,dp.拓展kmp,字典树.. . 字符串hash(模板) http://blog.csdn.net/gdujian ...
- BZOJ3786: 星系探索 Splay+DFS序
题目大意:给你一个树,支持三种操作,子树加,点到根的路径和,改变某一个点的父亲. 分析: 看起来像一个大LCT,但是很显然,LCT做子树加我不太会啊... 那么,考虑更换一个点的父亲这个操作很有意思, ...
- 【LeetCode148】Sort List★★bug
1.题目描述: 2.解题思路: 本题是要堆一个链表进行排序,并且要求时间复杂度为 O(n log n).很明显,要用到分治的思想,用二分法进行归并排序:找到链表的middle节点,然后递归对前半部分和 ...