要求

  • 给定一个含有 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. Web安全(更新中)

    sql注入 创建一个数据库 create database admin1; 查询数据库 查看所有数据库 show databases; 使用该数据库    use admin1; 创建一个表 创建一个 ...

  2. istio:在vs中实现ab测试和路径切割

    此篇内容 主要目的是总结vs中的match的有关规则和在istio中如何实现路径切割(当下版本1.8.2) 实验demo main.go package main import ( "git ...

  3. odoo视图入门学习- tree视图的使用

    上一篇内容:如何快速在odoo中创建自己的菜单 前言 上面的内容我们已经学会了如何去创建odoo的菜单,下面我们要学习的是odoo的基础视图tree视图,我们的目标是实现型号管理的列表页面 创建mod ...

  4. OO第一单元作业——魔幻求导

    简介 本单元作业分为三次 第一次作业:需要完成的任务为简单多项式导函数的求解. 第二次作业:需要完成的任务为包含简单幂函数和简单正余弦函数的导函数的求解. 第三次作业:需要完成的任务为包含简单幂函数和 ...

  5. 通过Dapr实现一个简单的基于.net的微服务电商系统

    本来想在Dpar 1.0GA时发布这篇文章,由于其他事情耽搁了放到现在.时下微服务和云原生技术如何如荼,微软也不甘示弱的和阿里一起适时推出了Dapr(https://dapr.io/),园子里关于da ...

  6. 6.1vector用法

    目录 一.用法介绍 二.基本用法 三.PAT A1039 一.用法介绍 vector<typename>name; 按照这样的格式进行定义与书写. 注意定义成双数组的情况要加上空格. ve ...

  7. css详解background八大属性及其含义

    background(背景) 以前笔者在css盒模型以及如何计算盒子的宽度一文中提到过盒模型可以看成由 元素外边距(margin).元素边框(border).元素内边距(padding)和元素内容(c ...

  8. 【macOS】显示/隐藏 允许“任何来源”的应用

    问题产生 在macOS中安装某些版本软件时会提示: "xxx"已损坏,打不开.您应该将它移动到废纸篓. 某些情况下实际上并不是软件已损坏,而是因为macOS对于开发者的验证导致软件 ...

  9. joda-time的简单使用及mysql时间函数的使用(今天,本周,本月)

    近期在做一些首页的统计数据复习了下mysql的时间函数,以及后续修改成 传入时间查询时使用的joda-time 软件简介 JodaTime 提供了一组Java类包用于处理包括ISO8601标准在内的d ...

  10. kubespray续签k8s证书

    查看证书过期时期 [root@node1 ~]# openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text |grep ' Not ...