当前位置:网站首页>leetcode 209. 长度最小的子数组
leetcode 209. 长度最小的子数组
2022-04-21 06:37:00 【小学五年级在读的蒟蒻】
leetcode 209. 长度最小的子数组
大家好,我是小学五年级在读的蒟蒻,专注于后端,一起见证蒟蒻的成长,您的评论与赞与关注是我的最大动力,如有错误还请不吝赐教,万分感谢。一起支持原创吧!纯手打有笔误还望谅解。
题目描述
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
题解一
使用for循环暴力求解
从数组第一个元素开始遍历数组,将每个元素的值相加,若遇到元素大于或等于给定值时则将相加的数组长度与原来已有的长度相比较按长度小的更新值,遍历完毕则判断是否与初始值一样,若不同则返回更新后的值,否则返回0
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int result=__INT32_MAX__;
int arrLength=0;
int sum;
for(int i=0;i<nums.size();i++){
sum=0;
for(int j=i;j<nums.size();j++){
sum+=nums[j];
if(sum>=target){
arrLength=j-i+1;
result=arrLength>result?result:arrLength;
break;
}
}
}
return result==__INT32_MAX__?0:result;
}
};
题解二
使用滑动窗口
将数据从首元素就开始相加,如果数据大于或小于给定值,则开始从头递减元素,然后不断更新符合条件的数组长度,最后判断是否长度与初始值一样,不一样返回更新后的值,否则返回0
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int result=__INT32_MAX__;
int arrLength=0;
int sum=0;
int j=0;
for(int i=0;i<nums.size();i++){
sum+=nums[i];
while (sum>=target)
{
arrLength=i-j+1;
result=arrLength<result?arrLength:result;
sum-=nums[j++];
/* code */
}
}
return result==__INT32_MAX__?0:result;
}
};
版权声明
本文为[小学五年级在读的蒟蒻]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44229867/article/details/122377553
边栏推荐
- NP, OSPF route aggregation
- Basic concepts of IP Multicast
- Leetcode 1387.将整数按权重排序(Sort Integers by The Power Value)
- leetcode 704·二分查找
- leetcode 707.设计链表
- 论文阅读:Measuring the Impact of a Successful DDoS Attack on the Customer Behaviour of Managed DNS Servi
- mysql底层的引擎选择,为什么索引结构选择B+树:(3.28-4.3)
- 数据库日志级别解析:(2022.2.21-2022.2.27)
- NP, NSSA area
- 2020-12-21至2022-1-2:解析观察者模式
猜你喜欢

Macro crystal micro ms9123, USB screen projection control chip, USB screen projector, support CVBS, S-Video video interface,

IGMP_ Huawei

Sort method ----- > Hill sort, heap sort

【WPF】数据模板选择器DataTemplateSelector

modern cryptography

Ms1836s, HDMI to CVBS, video converter, HDMI receiver, built-in MCU and memory

China Resources micro power amplifier cs3850eo, 2 × 40W class D audio power amplifier circuit, replacement: Zhipu core cs8673, tas5780 and tas5754 of Ti, domestic power amplifier

NP、NSSA区域

BGP authentication function

NP、OSPF路由聚合
随机推荐
Signals
【C#】LINQ
三层交换机与路由器对接上网
Solving 0 / 1 knapsack problem by dynamic programming
Domestic Poe, rpc304, replace ip802, ip804 and ip808, replace tps23861, ltc4292, pd69204 and si3459, start domestic Poe power supply and PSE, and ruipukang domestic Ethernet power supply and interface
Macro crystal micro ms9288, analog RGB (VGA) to HDMI, HDMI transmitter, audio decoder, built-in MCU
【WPF】转换器Converter
Blue Bridge Cup - palindromes and special palindromes
2020-12-24
NP, OSPF stub area
OSPF多区域
Font Icon website - common summary
【计组】性能
Troubleshooting NP and OSPF
【WPF】利用RadioButton制作导航条
【WPF】Popup
【网络协议】为什么要学习网络协议
NP, OSPF link state database
【计组】冯诺依曼体系结构
论文阅读:Domain Name Encryption Is Not Enough: Privacy Leakage via IP-based Website Fingerprinting