题目描述

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

示例:

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

解题思路

记录当前的连续子数组和,若大于等于s,则以当前子数组的最左端为基准向后遍历,若去掉此数后当前连续子数组和仍大于等于s,就把左端向右移动一位,否则更新当前的最小连续子数组长度。

代码

 class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int minCnt = INT_MAX, left = , right = , sum = ;
while(right < nums.size()){
sum += nums[right];
if(sum >= s){
while(left < right && sum - nums[left] >= s)
sum -= nums[left++];
minCnt = min(minCnt, right - left + );
}
right++;
}
if(minCnt == INT_MAX) return ;
return minCnt;
}
};

LeetCode 209. 长度最小的子数组(Minimum Size Subarray Sum)的更多相关文章

  1. [Swift]LeetCode209. 长度最小的子数组 | Minimum Size Subarray Sum

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

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

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

  3. 209. 长度最小的子数组--LeetCode

    来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/minimum-size-subarray-sum 著作权归领扣网络所有.商业转载请联系官方授权,非商业 ...

  4. Java实现 LeetCode 209 长度最小的子数组

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

  5. 双指针之滑动窗口(长度最小的子数组 和 和为s的连续正数序列)

    双指针之滑动窗口 (长度最小的子数组:和为s的连续正数序列) 1, 什么时候使用? (与子数组/字符串 有关的题目)~如果给了某个具体值的target,即用滑动窗口 不然就双指针(一般做法,左边< ...

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

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

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

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

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

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

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

随机推荐

  1. ORA-03113:通信通道的文件结尾 解决办法

    登录Oracle时出现错误:“ORA-03113:通信通道的文件结尾” 错误排查方法 Oracle出现错误,查看trace日志寻找问题根源:D:\oracle\diag\rdbms\orcl\orcl ...

  2. 【问题】man命令打开的手册上链接怎么展开?

    参考:How to follow links in linux man pages? 前言 在使用man查看命令帮助的时候,有些文字下面会有下划线.给人的感觉是一个链接,但是又打不开.那么到底是不是链 ...

  3. Pyspark读取csv文件

    #_*_coding:utf-8_*_ # spark读取csv文件 #指定schema: schema = StructType([ # true代表不为null StructField(" ...

  4. 1121 Django操作

    目录 Django前戏 一.课程导读 1.web应用 2.c/s b/s 架构 3.Python Web框架 二.原生socket服务 三.http协议 什么是http协议 四大特性 http工作原理 ...

  5. 2020年日期表-python实现

    import pandas as pdimport calendarimport datetime # 生成日期范围date = pd.date_range("2020-01-01" ...

  6. js 异步执行顺序

    参考文章: js 异步执行顺序   1.js的执行顺序,先同步后异步 2.异步中任务队列的执行顺序: 先微任务microtask队列,再宏任务macrotask队列 3.调用Promise 中的res ...

  7. 找到一些经验,关于使用thymeleaf时遇到的一些问题

    最近一直在使用spring boot,所以自然而然的使用了thymeleaf,但是我想说习惯了jsp之后使用thymeleaf真实觉得不顺手,在使用thymeleaf中也遇到了一些问题,在这里记录一下 ...

  8. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part4

    Load Data from Test Fixtures in Cypress When creating integration tests with Cypress, we’ll often wa ...

  9. ZooInspector使用

    一.工具 ZooInspector作用: 可以利用该工具图形化浏览ZK中的文件及文件夹 下载地址: https://issues.apache.org/jira/secure/attachment/1 ...

  10. 最短路--Dijkstra

    Dijkstra--单源最短路 算法思想 主要记住这句话:每次选择没有被访问过的,并且dis最小的点,加入集合,更新dis 模板 int dis[maxn],vis[maxn]; //距离,标记 vo ...