【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 ...
随机推荐
- 理解C#的Lock语法意义
一. 为什么要lock,lock了什么? 当我们使用线程的时候,效率最高的方式当然是异步,即各个线程同时运行,其间不相互依赖和等待.但当不同的线程都需要访问某个资源的时候,就需要同步机制了,也就是说当 ...
- Android 连续按两次back键退出系统
需求:在程序的首页,连续按两次back键退出Androd程序. 第一步:新建一个GApplication类继承Application,在GApplication中添加一个用于存放Activity的集合 ...
- phpstorm开发环境搭建流程
1.下载phpstorm 2.网上找注册码 phpstorm 8 license key Learn Programming===== LICENSE BEGIN =====63758-1204201 ...
- Linux内核RPC请求过程
这篇文章讲讲server端RPC报文的处理流程.server端RPC报文的处理函数是svc_process,这个函数位于net/sunrpc/svc.c中.这个函数须要一个svc_rqst结构的指针作 ...
- Django Rest Framework源码剖析(二)-----权限
一.简介 在上一篇博客中已经介绍了django rest framework 对于认证的源码流程,以及实现过程,当用户经过认证之后下一步就是涉及到权限的问题.比如订单的业务只能VIP才能查看,所以这时 ...
- [清华集训2015 Day2]矩阵变换-[稳定婚姻模型]
Description 给出一个N行M列的矩阵,保证满足以下性质: M>N. 矩阵中每个数都是 [0,N]中的自然数. 每行中, [1,N]中每个自然数刚好出现一次,其余的都是0. 每列中,[1 ...
- stm32f051 DMA需要注意的一点
STM32f051的DMA注意事项 问题说明:在使用ADC的DMA通道时,遇到了序列转换的乱序问题,我使用的是DMA循环模式,但是采集的数据却总是错的:第二个内存地址存放的是ADC序列转换中的第一个通 ...
- STEAM 自动安装时提示C++ 安装不了等问题
[情况] 今天安装游戏, 安装时自动安装 Visual C++ 2015 x64 Minimum Runtime ....不成功, 提示网络源不可使用, 或者使用以下安装源 C:\ProgramDat ...
- cocos2d-x学习记录6——自定义Button
cocos2d-x中封装CCMenuItem等相关按钮,但是有些时候需要自己封装按钮,这样能够更加灵活的实现对应功能. 自定义Button,需要重写OnEnter()和onExit()函数,并在对应函 ...
- Scrapyd+Gerapy部署Scrapy爬虫进行可视化管理
Scrapy是一个流行的爬虫框架,利用Scrapyd,可以将其部署在远程服务端运行,并通过命令对爬虫进行管理,而Gerapy为我们提供了精美的UI,可以在web页面上直接点击操作,管理部署在scrap ...