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的更多相关文章

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

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

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

  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】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...

  5. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  6. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

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

  8. 209. Minimum Size Subarray Sum(双指针)

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

  9. 209. Minimum Size Subarray Sum【滑动窗口】

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

随机推荐

  1. AddHandler php5-script .php\AddType text/html .php和AddType application/x-httpd-php .php的区别?

    让apache支持php文件的解释,有2种方法配置,RPM装的默认配置是:AddHandler php5-script .phpAddType text/html .php网上很多人的配置方法是:Ad ...

  2. python 通过shutil实现文件后缀名判断及复制

    In [18]: for file in os.listdir('.'): ...: if os.path.splitext(file)[1] == '.html': ...: print(file) ...

  3. OpenCV——图像的深度与通道数讲解

    矩阵数据类型: – CV_(S|U|F)C S = 符号整型 U = 无符号整型 F = 浮点型 E.g.: CV_8UC1 是指一个8位无符号整型单通道矩阵, CV_32FC2是指一个32位浮点型双 ...

  4. RC Calculation

    scenario 定义中包括 Mode.Corner.RC 其中 Corner (PVT)用于计算 cell delay 而 RC 用于计算 net delay 本文简要介绍如何使用 RC 参数来计算 ...

  5. JS省市区联动

    JS省市区使用文档 一:服务器返回JSON格式要求如下网址里面data的格式:(拿KISSY组件data格式来做的) http://gallery.kissyui.com/cityselector/d ...

  6. Python2.7-xdrlib

    xdrlib模块,用于打包和解包 xdr 数据.XDR 提供了一种与体系结构无关的表示数据,解决了数据字节排序的差异.数据字节大小.数据表示和数据对准的方式.使用XDR的应用程序,可以在异构硬件系统上 ...

  7. JAVA框架 Spring AOP注解

    一.准备工作: 1)导入jar包: 4个jar包. 2)约束:(spring需要所有的约束)有IOC约束和AOP 还有事务(tx)以及注解注入的约束(context). <?xml versio ...

  8. Xcode 备忘

    一. 打印一堆乱七八糟的东西: Edit Scheme... --> Run --> Arguments,在 Environment Variables 里添加 OS_ACTIVITY_M ...

  9. Java java.text.ParseException: Unparseable date

    用java将字符串转换成Date类型是,会出现java.text.ParseException: Unparseable date异常. 例如下面的这段代码就会出现上面的异常: public bool ...

  10. 保存网格(mesh)到磁盘上

    Unity提供了很方便的工具来保存mesh之类的,下面的代码挂在GameObject上后,按下F键能把mesh(该GameObject必须有mesh组件)保存到磁盘的Assets目录下.在磁盘上是.a ...