[leedcode 209] Minimum Size Subarray Sum
2015-08-04 22:36
239 查看
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.
For example, given the array
the subarray
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.
public class Solution { public int minSubArrayLen(int s, int[] nums) { //借助一个指针start,判断从start到i的sum是否大于s,如果大于,start++,并且不断更新minLen //注意,一开始需要将minLen初始化为Integer.MAX_VALUE int minLen=Integer.MAX_VALUE; int start=0; int sum=0; for(int i=0;i<nums.length;i++){ sum+=nums[i]; while(sum>=s){ minLen=Math.min(minLen,i-start+1); sum-=nums[start]; start++; } } return minLen==Integer.MAX_VALUE?0:minLen; } }
相关文章推荐
- java反射机制
- properties文件路径的读取
- sqlserver查询第2-4行数据
- 二阶切比雪夫多项式实现(scala版、python版)
- 2015080401 - 我只是敢和别人不一样随笔
- JavaBeans中的DAO
- ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets
- iOS中如何获取用户的触摸操作
- 2015 多校联赛 ——HDU5350(huffman)
- 推荐系统之协同过滤实现
- C语言的基本类型
- 最小生成树问题的常规分析过程
- 2015 多校联赛 ——HDU5350(huffman)
- 贝塞尔曲线的应用
- 超图的地图慧
- 分布式与集群的区别通俗解释
- 什么是逻辑分区管理 LVM ,如何在Ubuntu中使用?
- 什么是逻辑分区管理 LVM ,如何在Ubuntu中使用?
- swagger整合spring mvc教程
- 输入一个字符串,如果第一个字符是大写并且其他字符不是大写,那么输出true,否则输出false。