leetcode 209. 长度最小的子数组
2021-05-01 22:30
阅读:488
标签:pre 滑动 lse bar div 长度 整数 min size
题目
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组,并返回其长度。如果不存在符合条件的连续子数组,返回 0。
示例:
输入:s = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的连续子数组。
思路
滑动窗口模板题,不想解释
代码
class Solution { public: int minSubArrayLen(int s, vectorint>& nums) { int sum = 0; int ans = 0x3f3f3f; int l=0; for(int r=0;r) { sum += nums[r]; while(sum>=s) { ans = min(ans,r-l+1); sum -= nums[l]; l++; } } if(ans==0x3f3f3f) return 0; else return ans; } };
leetcode 209. 长度最小的子数组
标签:pre 滑动 lse bar div 长度 整数 min size
原文地址:https://www.cnblogs.com/simplekinght/p/13205568.html
上一篇:python学习第42天
下一篇:python基础语法
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:leetcode 209. 长度最小的子数组
文章链接:http://soscw.com/index.php/essay/81066.html
文章标题:leetcode 209. 长度最小的子数组
文章链接:http://soscw.com/index.php/essay/81066.html
评论
亲,登录后才可以留言!