Minimum Size Subarray Sum 滑动窗口移位
2015-06-09 14:07
274 查看
Minimum Size Subarray Sum
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
[2,3,1,2,4,3]and
s = 7,
the subarray
[4,3]has the minimal length under the problem constraint.
click to show more practice.
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { if(nums.size()==0) return 0; int left,right,sum,minLen=9999999; sum=0; for(left=0,right=0;right<nums.size();) { while(sum<s&&right<nums.size()) { sum+=nums[right]; right++; } if(sum>=s&&right-left<minLen) { minLen=right-left; } while(sum>=s) { sum-=nums[left]; left++; if(sum>=s&&right-left<minLen) { minLen=right-left; } } } if(minLen==9999999) return 0; return minLen; } };
相关文章推荐
- POJ 1491
- POJ 1488
- POJ 1503
- C and C++ 的困惑
- 年轻程序员的悲伤
- 2014年10月25日ACT考试写作真题及范文
- 驱动编译脚本
- poi导入图片到word
- Plus One
- 再造 “手机QQ” 侧滑菜单(一)——实现侧滑效果
- Leetcode[169]-Majority Element
- 大约ActionContext.getContext()使用体验
- 程序员保值的4个秘密
- 基于外部数据的用户信用评估
- POJ 1496
- C学习脚印
- CentOS系统的VPS安全设置与优化
- rpmbulid学习redis打包
- 模拟客户查看cdn节点的访问情况
- ABAP 上传、下载请求文件