当前位置:网站首页>Likou Brush Question Record 4.1-----209. The sub-array with the smallest length
Likou Brush Question Record 4.1-----209. The sub-array with the smallest length
2022-08-09 02:28:00 【@baigui】
一、题目
二、代码
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums)
{
//方法3:划窗
//我的理解是:swipe window shorter,The front is meaningless
//常规变量
int i=0;
int j=0;
int k=0;
//本题变量
int first_subscript=0; //头指针
int final_subscript=0; //尾指针
int return_length=INT32_MAX;
int size=nums.size();
double sum=0;
//尾指针不断后移
for(first_subscript=0;first_subscript<=size-1;first_subscript++)
{
sum=sum+nums[first_subscript];
while(sum>=target)
{
double temp_length=first_subscript-final_subscript+1; //After Satisfaction and Greater Than 比较长度
if(temp_length<return_length) return_length=temp_length;
sum=sum-nums[final_subscript];
final_subscript=final_subscript+1; //Try shortening the length
}
}
//if the length has not been modified 就赋值为0
if(return_length==INT32_MAX) return_length=0;
return return_length;
// //常规变量
// int i=0;
// int j=0;
// int k=0;
// //本题变量
// int size=nums.size();
// int minsize=0;
// int maxsize=0;
// int nowsize=0;
// int return_size=0;
// //First determine whether the upper limit is met The upper limit exists to be looked up 否则返回0
// double sum=0;
// double temp=0;
//方法一
//Traversing to find this length has a timeout issue
//Now the time complexity is N方 Binary search can becomeN*log2n A dichotomy should not be about finding a point Instead, find an interval
// for(i=1;i<=size;i++) //Find the length in turn1 2 3 4 .. and the largest array sumtarget比较
// {
// double sum=0;
// for(j=0;j<=(size)-i;j++)
// {
// double temp=0;
// for(k=j;k<=j+i-1;k++)
// temp=temp+nums[k];
// if(sum<temp) sum=temp;
// }
// if(sum>=target)
// {
// return_num=i;
// break;
// }
// }
//方法二
//第一次改进:Use binary search for length maxsize 和 minsize Continue to reduce the length of the interval 直到max和min重合 仍然超时 无语子
// for(i=0;i<=size-1;i++) temp+=nums[i];
// if(temp<target) //If the sum of all is less than the target value 返回值为0
// {
// return_size=0;
// }
// else //If the sum of all is greater than the target value 开始查找
// {
// maxsize=size;
// while(maxsize-minsize>=2)
// {
// nowsize=(maxsize+minsize)/2;
// sum=0;
// for(j=0;j<=(size)-nowsize;j++)
// {
// temp=0;
// for(k=j;k<=j+nowsize-1;k++)
// temp=temp+nums[k];
// if(sum<temp) sum=temp;
// }
// if(sum>=target) //如果和比目标值大 The median value is ok It can even be reduced down
// {
// maxsize=nowsize;
// }
// if(sum<target) //If the sum is compared to the target value The median value is not allowed must be added
// {
// minsize=nowsize+1;
// }
// }
// //跳出循环时,第一种情况 max和min相等 第二种情况 min比max小 maxYes, but because of the problem of binary values, it cannot be reduced
// if(minsize==maxsize) return_size=minsize; //相等直接返回
// if(minsize!=maxsize) //It's not equal to see if it's okay to be small Small can be returned Otherwise return large
// {
// sum=0;
// for(j=0;j<=(size)-minsize;j++)
// {
// temp=0;
// for(k=j;k<=j+minsize-1;k++)
// temp=temp+nums[k];
// if(sum<temp) sum=temp;
// }
// if(sum>=target) return_size=minsize;
// else return_size=maxsize;
// }
// }
}
};
三、运行结果
边栏推荐
猜你喜欢
【电商运营】不知道怎么做网站优化?这里有你需要知道的一切!
mysql 5.7 入坑
The most fierce "employee" in history, madly complaining about the billionaire boss Xiao Zha: So rich, he always wears the same clothes!
js实现数组去重的方式(7种)
OJ:L3-021 神坛 伪解 排序后遍历
不会吧!不会吧!居然还有人不知道重绘以及回流
SQLite切换日志模式优化
最近看到很多人想自学或者报班但是不清楚如何选择,我今天就和大家说说
时间复杂度和空间复杂度
ApiFile配置环境
随机推荐
D. Tournament Countdown
YOLOV1详解——Pytorch版
Flume (四) --------- Flume 企业开发案例
电磁辐射安全标准及检测方法
2020.12.4 log
数仓第二篇: 数据模型(维度建模)
自动化测试框架总结
313. 超级丑数-暴力解法
[LeetCode84双周赛] [模拟] 6174. 任务调度器 II,[贪心&数学] 6144. 将数组排序的最少替换次数
MT4/MQL4入门到精通外汇EA教程第一课 认识MetaEditor
gpio子系统和pinctrl子系统(下)
物联网未来:未来五年的预期
力扣刷题记录3.1-----977. 有序数组的平方
2022 Eye Care Products Exhibition, Beijing Eye Health Exhibition, Ophthalmology Exhibition, Myopia Correction Equipment Exhibition
ZCMU--5115: Buying Keys(C语言)
点击div内部默认文本被选中
Line segment tree of knowledge
Composer usage record
js实现数组去重的方式(7种)
最新工业界推荐系统数据集-召回排序模型原理、结构及代码实战整理分享