LeetCode 209 Minimum Size Subarray Sum
2024-08-26 18:28:25
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;
}
};
最新文章
- 如何开发一款堪比APP的微信小程序(腾讯内部团队分享)
- linux应用程序开发-进程通信(IPC)
- Python笔记总结week8
- python使用xlrd模块读写excel
- html5学习笔记4--API Range对象(一)
- metasploit连接数据库
- paper 97:异质人脸识别进展的资讯
- JSP 资源与网站
- 缓存之Memcached
- Smarty安装配置方法
- 【解决】笔记本发射WiFi
- 关于css3的rgba
- AngularJS的$http服务的应用
- 关于SubclassWindow()和SubclassDlgItem
- js正则判断电话/手机/邮箱/
- POJ9384 迷宫(基金会BFS)
- jQuery插件的编写相关技术 设计总结和最佳实践
- HTML在网页中插入音频视频简单的滚动效果
- 把项目中的那些恶心的无处存储的大块数据都丢到FastDFS之快速搭建
- Java_延迟操作
热门文章
- AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
- volatile修饰符
- oracleXE(oracle学习版)在windows的安装配置
- spring+springmvc+mybatis xml配置文件
- win7 安装好redis 如何安装扩展
- [HttpPost]和[AcceptVerbs(HttpVerbs.Post)]区别
- RabbitMQ 简介
- java 初始化顺序
- informatica读取FTP文件
- Handler sendMessage 与 obtainMessage (sendToTarget)比较