Minimum Size Subarray Sum -- leetcode
题目描写叙述:
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.
意思是讲给定一组整数数组nums和一个数字s。求的nums数组中最短长度的连续数字和,使得该和大于等于s。
比方上面实例数组 2 。3。1,2,4,3这一个数组中,大于等于7的最短连续为 3 ,4 那么 minimal为2
解题思路
定义一个start 。表示序列的事实上点,初始值为0
定义一个tempresult,用来累积连续序列和,初始值为0
定义一个minlength,用来表示最短序列。初始值为INT-MAX
一个循环,首先从数组開始处i=0,此时start=0,往后累积和,tempresult+=nums[i]
推断累积和和s的大小,假设大于等于s,则进行例如以下操作
- 先计算当前minlength=min(minlength,i-satrt+1) -然后将tempresult的值减去这个序列的nums[start]。起始点start++再次计算minlength(缩短整个序列),然后继续推断该值跟s的大小,直到减去的值使得tempresult的值小于s
假设小于s。则继续累积和。直到满足和大于等于s
注意。还有可能存在不存在序列的,就是整个序列和都相加都小于s。这是就须要推断(程序返回时候i-start==nums.size()&&tempresult小于s
首先 从2開始加,一直加到 2,3。1。2此时tempresult=8,大于等于7,先求的但当前minlength=4。然后缩短序列 start++,序列为3。1,2。对应的tempresult=6,小于7。则继续往后面累积和,3,1,2,4,然后缩短序列,变成,1,2,4,start++,此时minlength=3。然后继续缩短,2。4小于7,然后往后面继续累加,2,4,3,大于7,缩短,4,3,大于等于7,minlength=2。,到最后了。结束。
代码例如以下
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int i,start,minlength,tempresult;
minlength=INT_MAX;
start=0;
tempresult=0;
for(i=start;i<nums.size();i++)
{
tempresult+=nums[i];
if(tempresult>=s)
{
minlength=min(minlength,i-start+1);
tempresult-=nums[start];
start++;
while(tempresult>=s)
{
minlength=min(minlength,i-start+1);
tempresult-=nums[start];
start++;
}
}
}
if(i-start==nums.size()&&tempresult<s)
return 0;
return minlength;
}
};
Minimum Size Subarray Sum -- leetcode的更多相关文章
- Minimum Size Subarray Sum —— LeetCode
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- [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
Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- [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 ...
- 【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 ...
随机推荐
- 【无聊放个模板系列】HDU 1269 (SCC)
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...
- Delphi GDI+ Library
GDI+ LibraryThis library enables GDI+ functionality for Delphi 2009 and later. It differs from other ...
- php陷阱:字符串和数字比较
PHP中的比较运算符有点诡异,很容易出错,现列出比较规则: 1.当两个字符进行大小比较时,是比较着这两个字符的ASCII码大小——这条很容易理解. 2.当两个字符串进行大小比较时,是从第一个字符开始, ...
- VJP1218数字游戏(环形DP)
链接 数据比较小 直接爆了 5重 枚举断开的琏 dp[i][j][k] (i-j)区间为第k段 dp[i][j][k] = min(dp[i][j][k],dp[g][i-1][k-1]*s[i][j ...
- hdu1838Chessboard(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1838 其实原先不知道这题是DP 我都想不到DP去 想了想没思路 看了下题解 经典思路 :第n大的都是由第n-1大 ...
- Hadoop2.2编程:新旧API的区别
Hadoop最新版本的MapReduce Release 0.20.0的API包括了一个全新的Mapreduce JAVA API,有时候也称为上下文对象. 新的API类型上不兼容以前的API,所以, ...
- OpenSSL 拒绝服务漏洞
漏洞名称: OpenSSL 拒绝服务漏洞 CNNVD编号: CNNVD-201312-058 发布时间: 2013-12-05 更新时间: 2013-12-05 危害等级: 漏洞类型: 威胁 ...
- Hibernate: org.hibernate.exception.SQLGrammarException: could not insert: 错误
最近在学习Java Web,今天刚接触Hibernate.学习的书籍是<轻量级Java EE企业应用实战(第3版)>.书中367页5.2.2中给予的Hibernate例子中的代码运行有以下 ...
- 【 D3.js 选择集与数据详解 — 1 】 使用datum()绑定数据
选择集和数据的关系是 D3 最重要的基础,在[入门 - 第 7 章]时进行过些许讲解,对于要掌握好 D3 是远远不够的.故此开设一个新的分类,专门讨论选择集与数据的关系,包括数据绑定的使用和工作原理, ...
- 把USB打印机映射到LPT端口
把USB打印机映射到LPT端口(pos小票机测试成功)2010-12-23 18:11:00| 分类: 編程 | 标签: |字号大中小 订阅 注释:在DOS命令行下运行以下命令(以下为示例,根据实际情 ...