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. docker swarm英文文档学习-5-在swarm模式中运行Docker引擎

    Run Docker Engine in swarm mode在swarm模式中运行Docker引擎 当你第一次安装并开始使用Docker引擎时,默认情况下禁用swarm模式.在启用集群模式时,需要处 ...

  2. vagrant up下载box慢的解决办法

    即在运行vagrant up时得到其的下载路径,如: https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20190101.0.0/prov ...

  3. sublime出现 unable download.......

    I managed to fix this by changing my package settings. I made my osx downloader preference curl, and ...

  4. javascript实现拖曳与拖放图片

    javascript实现拖曳与拖放图片 其实对于drag和drop拖曳与拖放事件IE很早以前就支持这个操作了,我们先来看看HTML5中新增的拖放API. 在HTML5中想要实现拖放操作,至少要做以下操 ...

  5. aips初步设想

    2018年5月10日星期四 目标:设计一个高并发,高性能,可扩展的现代化综合大前置机. 具备以下功能: 协议支持:http.socket.mq 报文支持:xml.json.8583 Tps:500笔/ ...

  6. sphinx搜索 笔记

    架构图: 安装sphinx,见文章http://my.oschina.net/ptk/blog/495435 sphinx关键的配置文件.在里面写查询的sql. 两个关键命令:indexer生成查询索 ...

  7. C语言各种数据类型取值范围

    速查表: char -128 ~ +127 1Byte -2^7 ~ 2^7-1 unsigned char 0 ~ 255 1Byte 0 ~ 2^8-1 short -32767 ~ + 3276 ...

  8. Mike的农场 BZOJ4177

    分析: 最小割,不选则割的建模题...(然而一开始我当成了费用流,简直丧心病狂...最后想到了最小割...) 对于条件一,直接建一条双向边就可以了,并且不计入sum中,因为这是作为费用的存在,让它跑出 ...

  9. UML类图(Unified Modeling Language Class Diagrams)

    统一建模语言(UML) |  类图 什么是UML? UML是一种用于可视化描述系统,具有广泛用途的建模语言.作为一种标准化的图形语言,在软件工业中被用于软件系统部件的具体化,可视化,结构化描述以及撰写 ...

  10. Patchwork(2013年)--CNV检测方法流程

    文章题目:Patchwork: allele-specific copy number analysis of whole-genome sequenced tumor tissue 特点: 可以检测 ...