LeetCode 209 Minimum Size Subarray Sum
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;
}
};
LeetCode 209 Minimum Size Subarray Sum的更多相关文章
- [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- LeetCode 209. Minimum Size Subarray Sum (最短子数组之和)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- Java for LeetCode 209 Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【刷题-LeetCode】209. Minimum Size Subarray Sum
Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...
- LeetCode OJ 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 209. Minimum Size Subarray Sum(双指针)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- 【Leetcode】209. Minimum Size Subarray Sum
Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...
随机推荐
- 用flashfxp做ftp镜像同步
简单说,用flashfxp建立上传或者下载队列,然后设定一个定时任务来处理这个队列,就能同步下载或上传制定的目录了. 本人遇到的需求只需要做同步上传,记录如下: 开发者PC ---> 国内win ...
- Linux CentOS6.x ip设置(网卡设置)
修改IP永久生效按以下方法vi /etc/sysconfig/network-scripts/ifcfg-eth0(eth0,第一块网卡,如果是第二块则为eth1)按如下修改ip: DEVICE=et ...
- PHP基础之PDO
简介 PDO(PHP Data Object)是指PHP数据对象,它定义了一个轻量级的一致接口来统一操作各种数据库.PDO提供了一个数据访问抽象层,这意味着,不管使用哪种数据库,都可以用相同的函数(方 ...
- C# HttpWebRequest获取COOKIES
C# HttpWebRequest获取COOKIES byte[] bytes = Encoding.Default.GetBytes(_post); CookieContainer myCookie ...
- git pull和git fetch的区别
Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge Git fetch origin master git log ...
- Java面试常见问题汇总
1 String,StringBuffer与StringBuilder的区别?? String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非 ...
- .Net GridView 序号列
给GridView增加一列:序号列 <asp:TemplateField HeaderText="序号"> <ItemTemplate> <%# (( ...
- html5 第一天
html4与html5的琐碎比较,不全,第一次写,望多多包涵. 一 兼容性:html5在老版本的浏览器上也可以运行 二 实用性:HYML5都是封装的简单使用功能 三非革命性的发展 Html5向前兼容, ...
- #ThinkPHP_3.2.2模型# where查询条件汇总
特别喜欢 ThinkPHP_3.2.3 框架的Model,结合官方手册及查看源代码,汇总出其大体用法: 核心转换方法: $this->parseWhere($where); $whereStr ...
- Android如何一进入一个activity就唤醒键盘
方法总结: 在AndroidManife.xml中对应的的Activity配置中加入以下配置项: android:windowSoftInputMode="stateVisible|adju ...