要求

  • 给定一个含有 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. 解决wampserver 服务无法启动

    如图左击选中apache的httpd.conf把文本中的80端口,改成未被占用的端口.

  2. elementui 表格格式化

    <el-table-column prop="userType" label="角色" width="180" :formatter= ...

  3. matplotlib安装问题解决

    p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1) } span.s1 { font-variant-ligatures: no-c ...

  4. Mokito 单元测试与 Spring-Boot 集成测试

    Mokito 单元测试与 Spring-Boot 集成测试 版本说明 Java:1.8 JUnit:5.x Mokito:3.x H2:1.4.200 spring-boot-starter-test ...

  5. Dynamics CRM报表点击自动运行方法

    当我们点击了报表后一般会进入到条件筛选界面,再点击运行报表才可以直接运行报表.有一个方法可以点击报表后直接运行报表. 文本编辑器打开报表的rdl文件 找到如下位置的代码: 把Value部分改为: &l ...

  6. 8-50.Pow(x,n)

    题目描述: 解题思路: 第一想法是递归,结果f(x,n) = x * f(x,n-1);这种方法的空间复杂度太高了,太想当然. 看了下题解:采取分治的方法:f(x,n) = f(x,n/2) * f( ...

  7. JMeter 实战案例

    案例1:博客网站后端测试 案例2:JPetStore 应用 案例1:博客网站后端测试 测试目标 测试博客网站后端的常用 HTTP 接口的访问方法. 展示 HTTP 请求的各类使用方法. 展示提取 JS ...

  8. 10行C++代码实现高性能HTTP服务

    前言 是不是觉得C++写个服务太累,但又沉迷于C++的真香性能而无法自拔?作为一个老牌C++程序员(可以看我 github 上十几年前的C++项目:https://github.com/kevwan ...

  9. 微信小程序中的自定义组件

    微信小程序中的组件 前言 之前做小程序开发的时候,对于开发来说比较头疼的莫过于自定义组件了,当时官方对这方面的文档也只是寥寥几句,一笔带过而已,所以写起来真的是非常非常痛苦!! 好在微信小程序的库从 ...

  10. thinkphp5 ztree树形菜单

    教程:http://makaidong.com/zjfjava/4074_5873678.html 下载:https://github.com/zTree/zTree_v3