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 ...
随机推荐
- 来看看Meteor的功能
看了一上午,感觉这确实比所谓传统的APP开发,有很多不一样的地方. 记录下来: simple-todos.css /* CSS declarations go here */ /* CSS decla ...
- php重定向 htaccess文件的编写
主页的url重写规则:/controller/action.html(其中第一个英文代表控制器,第二个英文代表动作,映射到:index.php?c=controller&a=action) 后 ...
- char 与 unsigned char的本质区别
在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别 首先在内存中,char与unsigned char没有什么不同 ...
- jacob访问ocx控件方法和遇到的问题
最近在进行摄像机的二次开发,摄像机厂商提供了使用C++开发的ocx控件:所以尝试使用jacob来进行访问. 操作步骤如下: 1, 从官网(http://sourceforge.net/projects ...
- SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-004-Pizza例子的用户流程(flowExecutionKey、_eventId_phoneEntered、flowExecutionUrl )
一. 1. 2. 3.customer-flow.xml 自己定义customer,最后output <?xml version="1.0" encoding="U ...
- MinGW 编译libwebsockets
libwebsockets是一个轻量的纯C库,在这里尝试使用MinGW进行构建. 官网地址:http://libwebsockets.org/trac/libwebsockets下载地址:http:/ ...
- Visual Assist X
老师推荐的一款vc++6.0插件 visual assistx_百度百科 http://baike.baidu.com/view/4439152.htm?fr=aladdin 下载 VC++6.0助手 ...
- bzoj2298
不难发现必然是两个人之间话产生矛盾或自身话有问题很显然,当ai>aj时,若ai<n-bj时i,j两人话矛盾ai<ai<n-bj,这东西有没有数轴上的线段的既视感?我们只要求出做 ...
- -_-#【Cookie】缩小 Cookie
Reduce Cookie Size Cookie 是个很有趣的话题.根据 RFC 2109 的描述,每个客户端最多保持 300 个 Cookie,针对每个域名最多 20 个 Cookie (实际上多 ...
- 对easyUI中课堂源码编辑改进建议
在孙宇老师讲得Easyui第10讲完后,基本的增删该查做出来了,但是编辑存在一个问题:行内样式编辑修改,如果当用户没有修改数据,孙宇老师讲得时候直接return,这样做是不合理的:第二次再使用右键编辑 ...