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. Java中LinkedList的remove方法真的耗时O(1)吗?

    这个问题其实来源于Leetcode的一道题目,也就是上一篇日志 LRU Cache.在使用LinkedList超时后,换成ArrayList居然AC了,而问题居然是在于List.remove(Obje ...

  2. JavaC 编译目录下所有的UTF-8编码的java文件

    javac -encoding UTF-8  *.java

  3. DVB系统几种传输方式

    卫星 (DVB-S 及 DVB-S2)有线 (DVB-C)地面无线 (DVB-T)手持地面无线 (DVB-H)

  4. 目标检测的图像特征提取之(二)LBP特征

    LBP(Local Binary Pattern,局部二值模式)是一种用来描述图像年提出,用于纹理特征提取.而且,提取的特征是图像的局部的纹理特征: 1.LBP特征的描述 原始的LBP算子定义为在3* ...

  5. Java学习笔记之:Java封装

    一.引言 在面向对象程式设计方法中,封装(英语:Encapsulation)是指,一种将抽象性函式接口的实作细节部份包装.隐藏起来的方法. 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定 ...

  6. erp中三大订单CO、PO、MO各是代表什么?

    ERP即 企业资源计划 (Enterprise Resource Planning),由美国 Gartner Group 公司于1990年提出. ERP系统是指建立在信息技术基础上,以系统化的管理思想 ...

  7. Java API —— 反射

    1.类加载器     1)类的加载         · 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化.         · 加载 :就是 ...

  8. Shuffle和排序

    MapReduce确保每个reducer的输入都按键排序.系统执行排序的过程——将map输出作为输入传给reducer——称为shuffle.shuffle属于不断被优化和改进的代码库的一部分,从许多 ...

  9. 【问题】和NULL比较遇到的问题

    1.问题描述: select FName from teacher where FId not in( select distinct FTeacherId from student ) 子查询返回的 ...

  10. MFC中快速应用OpenCV教程

    论坛上看到非常经典的VS2008 + OpenCV 2.0下的配置过程: (这里用的是opencv2.0) 1. 文件 | 项目 | MFC | MFC应用程序 |(新名称如MFCtest)|next ...