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

    tarfile模块,读写 tar 压缩文件,包括用 gzip 或是 bz2 压缩的文件(如tar.bz2.tar.gz),一般使用 TarFile 类完成操作 1.模块方法 tarfile.is_ta ...

  2. Python2.7-os.path

    os.path 模块,实现了对文件路径的操作,但是不操作文件.由于不同系统文件路径格式不同,os.path 总是调用适合当前系统的版本,你也可以手动导入别的系统的(posixpath,ntpath,m ...

  3. C++之静态的变量和静态函数

    到目前为止,我们设计的类中所有的成员变量和成员函数都是属于对象的,如我们在前面定义的book类,利用book类声明两个对象Alice和Harry,这两个对象均拥有各自的price和title成员变量, ...

  4. day67

    昨日回顾 1 orm 创建表,新增字段,修改,删除字段,不能创建数据库  -字段属性phone=models.CharField(max_length=64,null=True)  -null=Tru ...

  5. jqgrid 启用键盘操作bindKeys

    给jqgrid启用键盘操作,代码如下: // the bindKeys() 启用键盘操作 $("#jqGrid").jqGrid('bindKeys'); 启动后,比如可以使用上下 ...

  6. odoo开发思路篇

    1.首先从客户那了解需求,知道他现有系统所能实现的功能,现在要求要实现的功能,至于怎样实现由我们自己去定.(拿到需求------->了解需求功能--------->自己实现的方法) 2.在 ...

  7. MySQL学习笔记04 插入中文时出现ERROR 1366 (HY000)

    1 环境: MySQL Server 6.0  命令行工具 2 问题 :  插入中文字符数据出现如下错误: ERROR 1366 (HY000): Incorrect string value: '\ ...

  8. Verilog使用相对路径时应注意的问题

    在Quartus编译环境下,使用include, fopen等文件操作指令时,会涉及到文件路径问题. 以 E:\quartus_project\sd_card_controller\rtl\sd_wb ...

  9. 网络对抗技术 2017-2018-2 20152515 Exp 8 Web基础

    1.本实践的具体内容: (1).Web前端HTML(0.5分) 输入命令apachectl start打开apahce,并查看端口号,确认apache开启: 在kali浏览器中输入localhost: ...

  10. C语言和python的区别

    Python可以说是目前最火的语言之一了,人工智能的兴起让Python一夜之间变得家喻户晓,Python号称目前最最简单易学的语言,现在有不少高校开始将Python作为大一新生的入门语言.本萌新也刚开 ...