要求

  • 给定一个含有 n 个正整数的数组和一个正整数 s
  • 找出该数组中满足其和 ≥ s 的长度最小的连续子数组
  • 如果不存在符合条件的连续子数组,返回 0

示例

  • 输入:s = 7, nums = [2,3,1,2,4,3]
  • 输出:2
  • 解释:子数组 [4,3] 是该条件下的长度最小的连续子数组

思路

  • 暴力解法(n3)
  • 滑动窗口(时间n,空间1)
    • 双索引,nums[l...r] 为滑动窗口
    • 小于s,j后移
    • 大于等于s,i后移
    • 每次移动后,若大于等于s,则更新最小长度res
    • L9:处理右边界到头的情况

 1 class Solution{
2 public:
3 int minSubArrayLen(int s, vector<int>& nums){
4 int l = 0, r = -1;
5 int sum = 0;
6 int res = nums.size() + 1;
7
8 while( l < nums.size()) {
9 if( r+1 < nums.size() && sum < s)
10 sum += nums[++r];
11 else
12 sum -= nums[l++];
13 if(sum >= s)
14 res = min(res,r-l+1);
15 }
16 if(res== nums.size()+1)
17 return 0;
18 return res;
19 }
20 };

延伸

  • 双索引技术

[刷题] 209 Minimum Size Subarray Sum的更多相关文章

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

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

  3. 209. Minimum Size Subarray Sum(双指针)

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

  4. LeetCode OJ 209. 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 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  6. 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 ...

  7. LeetCode 209 Minimum Size Subarray Sum

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

  8. Java for LeetCode 209 Minimum Size Subarray Sum

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

  9. 209. Minimum Size Subarray Sum

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

随机推荐

  1. RabbitMQ 入门 (Go) - 6. 数据持久化(上)

    从本节开始,我介绍一下如何将相关数据持久化到数据库,也就是上图中蓝色的部分. 目前的问题 我先运行 6 个传感器和2 个协调器,这里我使用了批处理文件: 运行后,看一下 RabbitMQ 的管理控制台 ...

  2. SQL语句练习(进阶版)

    学生数据库中有三个基本表(关系)如下: 学生表S(Sno,Sname,Age,Sex,SD) 课程表C(Cno,Cname, Teacher) 选课表SC(Sno,Cno,Grade) 请用SQL语言 ...

  3. Anacoda下报错conda command not found 以及TypeError: __new__() got an unexpected keyword argument 'serialized_options'

    在anacoda安装完成后,执行conda list命令,显示command not found 解决方法: 对于anaconda 2  , 输入export PATH=~/anaconda2/bin ...

  4. Array.prototype.fill 填充值被复用的问题

    考察如下示例代码: // 创建二维数组 const arr = Array(2).fill([]); // 操作第一个元素 arr[0].push(1); // 结果是操作了所有数组 console. ...

  5. Spring Boot 2.3 新特性优雅停机详解

    什么是优雅停机 先来一段简单的代码,如下: @RestController public class DemoController { @GetMapping("/demo") p ...

  6. matlab数值类型

    matlab数值类型 数值类型的分类 整数类型    整数类型有8种.上面的数字为其内存大小,如:int8,整数所占内存大小为8个字节.除了int64 和 uint64不能进行数值运算之外都可以. 类 ...

  7. 【Spring】循环依赖

    @ 目录 循环依赖 是什么? Spring是如何解决的? 源码分析 细节 循环依赖 是什么? ​ 简单的来说就是对象a的属性中引用了对象b,对象b的属性中引用了对象c......最后引用到a. < ...

  8. 大一那会,我用QQ远程帮同学考过计算机二级

    考证 大一那会儿流行考证,什么普通话.教师资格证.计算机.商务英语各种证五花八门的. 我们非计算机专业(我是通信工程)的基本上都会去考一个叫计算机二级的证书,说是找工作有用,大一新生,哪懂这些,一窝蜂 ...

  9. 什么是JWT?原理是什么?

    什么是JWT JWT 是Json Web Tokens的简称.用百度上面的解释讲,是目前流行的跨域认证解决方案,一种基于JSON的.用于在网络上声明某种主张的令牌(token). JTW原理 jwt验 ...

  10. 三个dom xss常用tips

    分享dom xss的三个案例 (1)javascript里面过滤单引号和双引号? 搭建环境: 只是过滤了单引号和双引号是可以xss的: 使用<>闭合script即可 </script ...