Problem:

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.

Summary:

找到数组中和大于目标值s的最短子序列。

Solution:

用两个指针分别代表当前子序列的开始和结尾,若计算出的sum小于s,则end++,否则start--,每计算出一个更短的子序列,更新res值。

 class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int len = nums.size();
int res = len + , start = , end = , sum = ;
while (start < len && end < len) {
while (sum < s && end < len) {
sum += nums[end++];
} while (sum >= s && start <= end) {
res = min(res, end - start);
sum -= nums[start++];
}
} return res == len + ? : res;
}
};

最新文章

  1. 如何开发一款堪比APP的微信小程序(腾讯内部团队分享)
  2. linux应用程序开发-进程通信(IPC)
  3. Python笔记总结week8
  4. python使用xlrd模块读写excel
  5. html5学习笔记4--API Range对象(一)
  6. metasploit连接数据库
  7. paper 97:异质人脸识别进展的资讯
  8. JSP 资源与网站
  9. 缓存之Memcached
  10. Smarty安装配置方法
  11. 【解决】笔记本发射WiFi
  12. 关于css3的rgba
  13. AngularJS的$http服务的应用
  14. 关于SubclassWindow()和SubclassDlgItem
  15. js正则判断电话/手机/邮箱/
  16. POJ9384 迷宫(基金会BFS)
  17. jQuery插件的编写相关技术 设计总结和最佳实践
  18. HTML在网页中插入音频视频简单的滚动效果
  19. 把项目中的那些恶心的无处存储的大块数据都丢到FastDFS之快速搭建
  20. Java_延迟操作

热门文章

  1. AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
  2. volatile修饰符
  3. oracleXE(oracle学习版)在windows的安装配置
  4. spring+springmvc+mybatis xml配置文件
  5. win7 安装好redis 如何安装扩展
  6. [HttpPost]和[AcceptVerbs(HttpVerbs.Post)]区别
  7. RabbitMQ 简介
  8. java 初始化顺序
  9. informatica读取FTP文件
  10. Handler sendMessage 与 obtainMessage (sendToTarget)比较