LeetCode Minimum Size Subarray Sum (最短子序列和)
题意:给一个序列,找出其中一个连续子序列,其和大于s但是所含元素最少。返回其长度。0代表整个序列之和均小于s。
思路:O(n)的方法容易想。就是扫一遍,当子序列和大于s时就一直删减子序列前面的一个元素,直到小于s就停下,继续累加后面的。
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int ans=0x7fffffff, cnt=, sum=;
for(int i=; i<nums.size(); i++)
{
cnt++;//记录个数
sum+=nums[i];
while(sum>=s)
{
ans=min(ans,cnt);//答案
sum-=nums[i-cnt+];//去掉子序列最前面的元素
cnt--;
}
}
if(ans<0x7fffffff) return ans;
else return ;
}
};
AC代码
LeetCode Minimum Size Subarray Sum (最短子序列和)的更多相关文章
- [LeetCode] 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 解题思路
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 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
- (leetcode)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
题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- 【刷题-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] Maximum Size Subarray Sum Equals k 最大子数组之和为k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
随机推荐
- PHPstorm 的快捷键
// ctrl+shift+n 查找文件// ctrl+j 插入活动代码提示// ctrl+alt+t 当前位置插入环绕代码// alt+insert ...
- PL/SQL中如何执行DDL、SCL?
PL/SQL程序中不能直接执行DDL语句.为什么? 假设我们在pl/sql程序中有这样的一条DDL语句—— drop table emp:在第一次解析pl/sql中的“drop table emp;” ...
- MVC5+EF6+BootStrap3.3.5 博客系统之项目搭建(一)
环境:vs2013,sql2008R2 引用版本:MVC5,EF6,BootStrap3.3.5 在之前一直都是webfrom开发,虽然开发简单:但是有很多不足的地方.在之前开发都是webfrom+M ...
- ExtJS 添加图标icon
extjs控件有两个属性:一个是iconCls:另一个是icon.通过这两个属性可以对控件添加图标 1.直接引用图标路径 icon: '../icons/application_view_detail ...
- shutdown彻底关闭tomcat,以及多线程关闭
最近做的一个Web项目,发现shutdown.sh后,无法关掉tomcat进程. ps -ef | grep tomcat 返回tomcat进程仍然存在.经过调查发现是因为在Web应用中启动了线程池, ...
- 用Canvas制作小游戏——贪吃蛇
今天呢,主要和小伙伴们分享一下一个贪吃蛇游戏从构思到实现的过程~因为我不是很喜欢直接PO代码,所以只copy代码的童鞋们请出门左转不谢. 按理说canvas与其应用是老生常谈了,可我在准备阶段却搜索不 ...
- resin access.log format配置详解
The access log formatting variables follow the Apache variables: %b result content length %D tim ...
- 友盟分享在appdelegate中的调用语句举例
//友盟 [UMSocialData setAppKey:UmengAppkey]; [UMSocialConfig setSupportedInterfaceOrientations:UIInter ...
- dubbo zookeeper模型
本文摘自dubbo的官方文档,原文请参见: http://alibaba.github.io/dubbo-doc-static/User+Guide-zh.htm#UserGuide-zh-Zooke ...
- IntPtr
一:什么是IntPtr 先来看看MSDN上说的:用于表示指针或句柄的平台特定类型.这个其实说出了这样两个事实,IntPtr 可以用来表示指针或句柄.它是一个平台特定类型.对于它的解释,这个哥们写的比较 ...