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.

题意:

  求一个给定数组的部分连续和Sum,要求Sum大于给定数值S。返回满足条件的连续和Sum中长度最短的。

思路:

  通过标记起始位置(p)、终止位置(q)构建一个滑动“窗口”,使“窗口”内元素的和Sum始终保持小于s。若从(q)端新加元素使得Sum >= s,则更新Sum的最小长度值_min,并从(p)端删除元素,保持Sum < s。

 class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) { const int len = nums.size(); if(len == )
return ; //初始化,_min初始化为len+1(上界)
int p = , q = , sum = , _min = len + ; for(; q < nums.size(); q++)
{
//顺序扫描数组,从q端添加元素
sum += nums[q]; //保持“窗口”值小于s
while(sum >= s)
{
if((q - p) < _min)
_min = q - p; //从p端删除元素
sum -= nums[p++];
}
} return _min > len ? : _min + ;
}
};

【LeetCode 209】Minimum Size Subarray Sum的更多相关文章

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

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

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

  3. 【数组】Minimum Size Subarray Sum

    题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...

  4. LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)

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

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

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

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

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

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

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

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

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

随机推荐

  1. Oracle 6 - 锁和闩 - transaction的可串行化

    本文主要内容 1.transaction的可串行化 2.数据库并发带来的问题, dirty read, Nonrepeatable reads, Phantoms幻读 3.隔离级别和2中的问题 4. ...

  2. DP:LCS(最长公共子串、最长公共子序列)

    1. 两者区别 约定:在本文中用 LCStr 表示最长公共子串(Longest Common Substring),LCSeq 表示最长公共子序列(Longest Common Subsequence ...

  3. s.charAt()

    public class ish{public static void main(String[]args){ String s="call me ishmae";System.o ...

  4. Android模拟器问题:No system images installed for this target

    CPU/ABI选项无法选择,提示:No system images installed for this target,也就是没有适合的系统镜像 打开Android Manager SDK 下载完后重 ...

  5. PowerDesigner概念模型的Notation设置

    原文:PowerDesigner概念模型的Notation设置 在进行数据库设计模型时,分为概念模型设计和物理模型设计两种,概念模型主要是反映真是 世界中的业务关系,也就是我们常用的实体关系图.物理模 ...

  6. C# 使用ManualResetEvent 进行线程同步

    上一篇我们介绍了AutoResetEvent,这一篇我们来看下ManualResetEvent ,顾名思义ManualResetEvent  为手动重置事件. AutoResetEvent和Manua ...

  7. apk反编译(1)用apktool破解apk

    1,下载 http://ibotpeaches.github.io/Apktool/ 2,破解 把下载的apktool_2.0.3.jar 和 weixin638android680.apk  拷贝到 ...

  8. 演练:Office 编程(C# 和 Visual Basic)

    https://msdn.microsoft.com/zh-cn/library/ee342218(v=vs.110).aspx PIA的全称是 primary interop assembly  主 ...

  9. 异常:java.lang.UnsupportedOperationException: Manual close is not allowed over a Spring managed SqlSession

    使用mybatis-3.2.2.jar + mybatis-spring-1.2.0.jar集成时,报以下异常: 15:42:48.538 [Thread-1] DEBUG o.s.b.f.s.Dis ...

  10. STL源码中map和set中key值不能修改的实现

    前言 最近正好刚刚看完,<stl源码剖析>这本书的map和set的源码部分.但是看完之后又突然发现,之前怎么没有注意到map和set容器中key不能修改是怎么实现的.故,特此整理如下. s ...