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 ...
随机推荐
- Oracle 内核参数
安装Oracle的时候,可以参考Oracle 的安装文档,来设置相关内核参数的值,但是有些参数的值还是需要根据我们自己的情况来进行调整.注:不同系统的参数不同,本篇针对linux. 一.Linux 系 ...
- jooml二次开发---添加文章组件
在写一个joomla组件的时候需要手动添加excel表格,并把表格当做文章的内容添加到前台文章中, 开始不知道怎么下手,索性先把一个基本的组件写出来,在joomla网站上测试是可以访问这个组件的,在p ...
- orcale同一条语句运行速度差异问题
在oracle中执行一个查询语句,该语句首次执行时orcale会把其内容存入SGA(系统全局区)中,下次再执行同一条语句的时候就不再去解析该语句,而是直接从SGA区中取出该语句执行,但是前提是SQL没 ...
- Mysql数据库备份和还原常用的命令
Mysql数据库备份和还原常用的命令是进行Mysql数据库备份和还原的关键,没有命令,什么都无从做起,更谈不上什么备份还原,只有给系统这个命令,让它去执行,才能完成Mysql数据库备份和还原的操作,下 ...
- centos修改ssh端口
1.编辑防火墙配置:vi /etc/sysconfig/iptables防火墙增加新端口2222-A INPUT -m state --state NEW -m tcp -p tcp --dport ...
- 2016 系统设计第一期 (档案一)MVC 引用 js css
@Styles.Render("~/Bootstrap/css/bootstrap-theme.css") @Scripts.Render("~/jQuery/jquer ...
- EXTJS 4.2 资料 控件之Grid 行编辑绑定下拉框,并点一次触发一次事件
主要代码: { header: '属性值', dataIndex: 'PropertyValueName', width: 130, editor: new Ext.form.field.ComboB ...
- 开发之前的思考-UI结构设计
UI结构设计遵循的一些要点 1.尽量不要让UI作为Camera的子物体 因为UI和摄像机敏感的关系,尽量不要将UI作为摄像机的子物体,避免出现一些因为透视(3D UI)等问题导致的视觉Bug. 2.尽 ...
- 使用tolua++编译pkg,从而创建自定义类让Lua脚本使用
步骤一:首先自定义类(这里Himi自定义类名 “MySprite”) MySprite.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // // ...
- ACM题集以及各种总结大全!
ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...