Leetcode172: Minimum Size Subarray Sum
2015-11-26 16:00
190 查看
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
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) { int n = nums.size(); int i = 0; int j = 0; int minlen = n+1; int sum = 0; while(i <= j && j < n && sum < s) { sum += nums[j]; while(i <= j && sum >= s) { minlen = min(minlen, j-i+1); sum -= nums[i]; i++; } j++; } return minlen == n+1 ? 0:minlen; } };
相关文章推荐
- Java 与无符号那些事儿
- struts2 流程分析
- makefile
- OpenGL ES雾
- python自学笔记 20151126
- C++primer plus第六版课后编程练习答案6.5
- Nginx负载均衡配置实例详解
- 在PHP语言中使用JSON
- 汉字转化成拼音
- 搜索引擎关键字智能提示的一种实现
- 62、常规控件(5)Navigation View –美观的侧滑视图
- 大学生字体如"打印体" 网友称:太强大
- Hadoop中hdfs的错误
- 详解Java的Struts框架中栈值和OGNL的使用
- Matlab中的randperm和randsample函数用法总结
- android Log.isLoggable步骤的使用
- Framework3.5 最终文件下载及离线安装
- 设计模式之--原型模式
- spring aop 配置使用说明
- 【同步复制常见错误处理4】SQL SERVER同步环境新增发布对象时不能生成(sp_MS+表名)同步存储过程