当前位置:网站首页>力扣209. 长度最小的子数组
力扣209. 长度最小的子数组
2022-06-30 04:45:00 【智慧的人不要秃头】
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-size-subarray-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法:滑动窗口(窗口大小不定)
class Solution {
public:
/*暴力解法:不推荐,时间复杂度为O(n^2),力扣上不会通过,超出时间限制
时间复杂度:O(n^2) 空间复杂度:O(1)*/
/*
int minSubArrayLen(int target, vector<int>& nums) {
int result = INT32_MAX; //最终的结果,长度最短的连续子数组
int subLength = 0; //当前遍历到的子序列的数值之和
int size = nums.size();
for(int i = 0; i < size; ++i) { //设置子序列int sum = 0;
for(int j = i; j < size; ++j) { //设置子序列终点为j
sum += nums[j];
if(sum >= target) { //一旦满足条件就更新result的大小
subLength = j - i + 1;
result = result < subLength ? result : subLength;
break; //已经找到了当前最短的子序列,所以直接break
}
}
}
//如果result没有被赋值,说明没有满足条件的连续子序列,返回0
return result == INT32_MAX ? 0 : result;
}
*/
/*推荐解法:滑动窗口*/
int minSubArrayLen(int target, vector<int>& nums) {
int result = INT32_MAX; //返回的结果,初始先设为最大值,后面不断的更新它
int subLength = 0; //当前遍历到的满足返回条件的连续子序列的长度,滑动窗口的长度
int size = nums.size(); //给定的数组的大小
int i = 0; //滑动窗口的起始位置
int sum = 0; //当前遍历到的序列的加和,滑动窗口数值之和
for(int j = 0; j < size; ++j) {
sum += nums[j];
//这里一定要使用while不能使用if,每次更新i(起始位置),要不断比较子序列是否符合条件
while(sum >= target) {
subLength = j - i + 1; //取子序列的长度
result = result < subLength ? result : subLength;
sum -= nums[i++]; //滑动窗口的精髓,不断地变更i起始位置
}
}
//如果result没有被赋值,说明没有满足条件的连续子序列,返回0
return result == INT32_MAX ? 0 : result;
//时间复杂度:O(n) 空间复杂度:O(1)
}
};
边栏推荐
- 0 basic unity course. Bricklaying
- National Museum of Singapore - give you spiritual and physical satisfaction
- Directory operations and virtual file systems
- 【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
- Dual domain SSL certificate
- Enlist soldiers and generals, draw small programs, multi-threaded display time
- Sectigo certificate
- The role of break
- Unreal 4 learning notes - data storage using blueprints
- Unity a* road finding force planning
猜你喜欢

【Paper】2019_ Distributed Cooperative Control of a High-speed Train

【Paper】2019_ Consensus Control of Multiple AUVs Recovery System Under Switching Topologies and Time D

Foreign SSL certificate

【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator

Redis implements SMS login function (II) redis implements login function

The most comprehensive summary notes of redis foundation + advanced project in history

Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)

What is multimodal interaction?

Connect to the database and run node JS running database shows that the database is missing

How to renew an SSL certificate
随机推荐
Redis implements SMS login function (II) redis implements login function
IIS request SSL certificate
Unity realizes rotation and Revolution
BeanFactory创建流程
Bean creation process and lazy init delay loading mechanism
One interview question a day the difference between B tree and b+ tree
Collective system
What is an optocoupler circuit and what should be paid attention to in actual use?
Connect to the database and run node JS running database shows that the database is missing
Why does the computer have no network after win10 is turned on?
My idea configuration
Array of small C
Issue SSL certificate with IP address
File and IO
File system and directory operations
Serializable and Deserialize
【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
Mongodb learning
為什麼win10開熱點後電腦沒有網絡?
Qos(Quality of Service)