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. 安装的 Linux 软件包有多少?

    导读 你有没有想过你的 Linux 系统上安装了几千个软件包? 是的,我说的是“千”. 即使是相当一般的 Linux 系统也可能安装了上千个软件包. 有很多方法可以获得这些包到底是什么包的详细信息. ...

  2. JS 仿腾讯发表微博的效果

    JS 仿腾讯发表微博的效果 最近2天研究了下 腾讯发表微博的效果 特此来分享下,效果如下: 在此分享前 来谈谈本人编写代码的习惯,很多人会问我既然用的是jquery框架 为什么写的组件不用Jquery ...

  3. windows10 安装 Anaconda 并配置 pytorch1.0

    官网下载Anaconda安装包,按步骤安装即可安装完后,打开DOS,或Anaconda自带的Anaconda Prompt终端查看Anaconda已安装的安装包C:\Users\jiangshan&g ...

  4. js基础知识入门总结

    1.第一个js程序 一个项目包括三部分:前端(html.css.js).数据库.后端技术 引入方式:页面中直接写,script标签引入 js事件绑定: <input type="but ...

  5. 3-51单片机ESP8266学习-AT指令(学会刷固件)

    前言:体验一下刷固件(给单片机更新程序) 上一篇链接  http://www.cnblogs.com/yangfengwu/p/8757036.html 源码链接:https://pan.baidu. ...

  6. day48

    flex布局 响应式布局 过度 动画 flex布局 学习目的:基于之前所学的盒模型布局(display).浮动布局(float).定位布局(position),都不能很好的解决block垂直居中的问题 ...

  7. drupal 7 连接多个数据库

    Drupal7系统,重写了数据库操作内核,其强大的功能无需多言.一次偶然的机会,需要提取Drupal默认安装数据库之外的一个数据库中的数据 ,可谓是绞尽脑汁,上网查阅最后终于找到了一个笨而又合适的方法 ...

  8. 【转】PHP之FastCGI与mod_php详解

    原文地址:http://article.gitos.cn/2015/Aurthur/PHP-Mod-PHP-And-Fast-CGI-Explain.html 背景 PHP最常用的方式是以模块的方式( ...

  9. Nginx 服务器的安装部署(CentOS系统)

    1.准备安装环境yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-develgcc编译器 ...

  10. 从零开始学cookie(个人笔记)——一

    未完待续 参考链接 : cookie (储存在用户本地终端上的数据) 关键词: cookie session HTTP 小文本文件 解释 Cookie 是由 Web 服务器保存在用户浏览器上的小文本文 ...