leetcode.209. Minimum Size Subarray Sum
2016-05-09 22:57
99 查看
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
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
[2,3,1,2,4,3]and
s = 7,
the subarray
[4,3]has the minimal length under the problem constraint.
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { if(nums.size()==0||s<0) return 0; int start = 0; int end = 0; int sum = 0; int len = INT_MAX; while(start<nums.size() && end<nums.size()) { while(sum<s && end<nums.size()) { sum += nums[end++]; } while(sum>=s && start<=end) { len = min(len,end-start); sum -= nums[start++]; } } return len == INT_MAX ? 0 : len; } };
相关文章推荐
- 单例模式与垃圾回收 学习笔记
- Mysql 优化原则
- 算法导论 最长公共子序列的c程序实现带主函数
- linux基础(14)--输入输出重定向、文件查找--RHEL6.5
- Android 开发入门之《第一行代码》(1)
- java 线程四
- c++实验5-求和
- 用JAVA实现一个爬虫,爬取知乎的上的内容
- Spring IOC容器-注解的方式【更简化】
- 用vscode写博客和发布
- CDOJ 1325 卿学姐与基本法 (离散化+区间修改)
- textview删除线
- java 线程三
- linux下面mongodb的安装
- 《java入门第一季》之类StringBuffer类初步
- Android播放声音文体
- 《java入门第一季》之类StringBuffer类初步
- GeekBand C++面向对象高级编程(上)一
- CTF中的EXP编写技巧 zio库的使用
- 第2章 面向对象的设计原则(SOLID):3_依赖倒置原则(DIP)