题目描写叙述:

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

拿上面的数组列子进行解说nums= 2 ,3,1,2,4。3,s=7

首先 从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的更多相关文章

  1. Minimum Size Subarray Sum —— LeetCode

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  2. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  3. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  4. 【刷题-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 ...

  5. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  6. 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. jenkin系列_调度jmeter实现分布式测试

    假设现在有 192.168.1.100(jmeter 控制器 C ).192.168.1.101(jmeter负载机 B)两台机器进行分布式测试,各个步骤如下 1. C 和B 安装jmeter,并运行 ...

  2. MySql 命令积累

    一. 修改表的自增起点 ; 二.获取自增键值 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID ...

  3. hadoop2.2编程:MRUnit

    examples: Overview This document explains how to write unit tests for your map reduce code, and test ...

  4. NOI2010超级钢琴 2

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 1296  Solved: 606[Submit][Status ...

  5. AFNetworking 2.0 获取json数据时,返回 NSLocalizedDescription=Request failed: unacceptable content-type: text/html, 解决方法.

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSe ...

  6. 结合Zen Coding快速编写HTML代码(sublime text2篇)

    首先说下,如何在sublime text2加上zen coding插件 按 Ctrl+`(就是~这个键) 复制下面的代码 确认 重新启动sublime text2 import urllib2,os; ...

  7. MVC Action 返回类型[转]

    一.         ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回 ...

  8. 谈谈分布式事务之二:基于DTC的分布式事务管理模型[下篇]

    [续上篇] 当基于LTM或者KTM的事务提升到基于DTC的分布式事务后,DTC成为了本机所有事务型资源管理器的管理者:此外,当一个事务型操作超出了本机的范 围,出现了跨机器的调用后,本机的DTC需要于 ...

  9. 谈谈分布式事务之一:SOA需要怎样的事务控制方式

    在一个基于SOA架构的分布式系统体系中,服务(Service)成为了基本的功能提供单元,无论与业务流程无关的基础功能,还是具体的业务逻辑, 均实现在相应的服务之中.服务对外提供统一的接口,服务之间采用 ...

  10. ADO.NET——获取output 和 return值

    程序代码 //存储过程 //Create PROCEDURE MYSQL // @a int, // @b int //AS // return @a + @b //GO SqlConnection ...