209. Minimum Size Subarray Sum
2016-03-16 17:08
330 查看
题目:
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
click to show more practice.
More practice:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
解题思路:
利用滑动窗口的思想
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.
More practice:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
解题思路:
利用滑动窗口的思想
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int i=0,j=0; int sum = 0,min=INT_MAX; while(j<nums.size()) { while(j<nums.size() and sum<s)sum += nums[j++]; if(sum>=s) { while(sum>=s and i<j) sum -= nums[i++]; min = min<(j-i+1) ? min:j-i+1; } } return min==INT_MAX ? 0:min; } };
相关文章推荐
- Mybatis与Hibernate的区别
- 在线的免费Markdown编辑器 'MaHua'-(麻花) & MarkDown学习资料
- VS2010 OPENGL环境配置
- CF 632 D 技巧题
- Bzoj3124 [SDO2013]直径
- Material Design (一),NavigationView+DrawerLayout轻松实现侧拉菜单
- MJRefresh的简单使用
- 抽象工厂模式
- 设计模式 之 装饰者(Decorator)模式
- lintcode:带环链表
- java虚拟机工具入门
- HDOJ-2838 Cow Sorting
- Kerberos认证浅析
- java虚拟机工具入门
- iOS 封装一个 Date处理服务器中返回的时间
- 如何阅读jQuery源码整体架构
- ConurrentHashMap和Hashtable的区别
- APT源
- RecyclerView调用smoothScrollToPosition() 控制滑动速度
- MySQL日志