[刷题] 209 Minimum Size Subarray Sum
要求
- 给定一个含有 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的更多相关文章
- 【刷题-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 ...
- [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 ...
- 209. Minimum Size Subarray Sum(双指针)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- 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 ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- 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 ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 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 ...
- 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- 数据表设计之主键自增、UUID或联合主键
最近在做数据库设计的时候(以MySQL为主),遇到不少困惑,因为之前做数据库表设计,基本上主键都是使用自增的形式,最近因为这种做法,被领导指出存在一些不足,于是我想搞明白哪里不足. 一.MySQL为什 ...
- Apache SkyWalking 告警配置指南
Apache SkyWalking Apache SkyWalking是分布式系统的应用程序性能监视工具(Application Performance Management,APM),专为微服务.云 ...
- day-02-循环
while 循环 why:大气循环, 吃饭,上课,睡觉,日复一日,歌曲列表循序环,程序中:输入用户名密码, what:while 无限循环. how: 基本结构: while 条件: 循环体 初识循环 ...
- Dynamics CRM安装教程九(续):自建证书的CRM项目客户端设置CRM访问
配置完IFD之后就可以为客户端电脑配置访问CRM了首先到CA证书服务器中把证书下载下来,打开CA服务器的浏览器,输入地址http://stg-ad/certsrv/ 其中stg-ad是机器名之后点击下 ...
- JavaWeb 补充(JSP&EL&JSTL)
1. JSP: 1. 指令 2. 注释 3. 内置对象 2. MVC开发模式 3. EL表达式 4. JSTL标签 5. 三层架构 JSP: 1. 指令 * 作用:用于 ...
- house_of_storm 详解
house_of_storm 漏洞危害 House_of_storm 可以在任意地址写出chunk地址,进而把这个地址的高位当作size,可以进行任意地址分配chunk,也就是可以造成任意地址写的后果 ...
- Spring-Gateway与Spring-Security在前后端分离项目中的实践
前言 网上貌似webflux这一套的SpringSecurity操作资料貌似很少. 自己研究了一波,记录下来做一点备忘,如果能帮到也在迷惑的人一点点,就更好了. 新项目是前后端分离的项目,前台vue, ...
- Borrowers UVA - 230
I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shel ...
- [BUAA2021软工助教]案例分析作业总结
目录 一.作业链接 二.优秀作业推荐 A+作业推荐 A作业推荐 三.总结 所有案例分析总结 特色与优点 问题与建议 不同类产品案例分析Bug汇总 CSDN问答社区.Stack Overflow.Seg ...
- nginx下强制跳转到www域名
跳转www #先监听 exp.com域名,然后转发到www下面 server { listen 80; server_name exp.com; rewrite ^(.*) $scheme://www ...