当前位置:网站首页>leetcode 209. Minimum length subarray
leetcode 209. Minimum length subarray
2022-04-21 07:46:00 【Konjaku in Grade 5 of primary school】
leetcode 209. Subarray with the smallest length
Hello everyone , I'm studying konjak in grade five of primary school , Focus on the back end , Witness the growth of konjaku together , Your comments, praise and attention are my biggest motivation , If you have any mistakes, please let me know , Thank you very much . Let's support originality ! There is a mistake in writing .
Title Description
Given a containing n An array of positive integers and a positive integer target .
Find the sum of the array ≥ target The smallest length of Continuous subarray [numsl, numsl+1, …, numsr-1, numsr] , And return its length . If there is no sub array that meets the conditions , return 0 .
Answer 1
Use for Circular violence
Traverse the array from the first element of the array , Add the values of each element , If the element is greater than or equal to the given value, the added array length will be compared with the original length, and the value will be updated according to the smaller length , After traversal, judge whether it is the same as the initial value , If it is different, the updated value will be returned , Otherwise return to 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;
}
};
Explanation 2
Using the slide window
Add the data from the first element , If the data is greater than or less than the given value , Start decrementing elements from scratch , Then continuously update the length of the qualified array , Finally, judge whether the length is the same as the initial value , Different, return the updated value , Otherwise return to 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;
}
};
版权声明
本文为[Konjaku in Grade 5 of primary school]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210626569396.html
边栏推荐
- Flutter初体验
- DeprecationWarning: NewId() is deprecated
- ReportViewer的RDLC打印报表怎么动态加载参数、图片、背景图
- GoLang项目开发基础
- 动态生成三级菜单
- Nsctf - some topics WP
- 设置谷歌浏览器深色黑色背景
- URL Parsing
- About DM Dameng database, SQL to obtain user table information, data table structure, data table creation statement, primary key and other information
- Ruijie eg easy gateway remote command execution vulnerability-1
猜你喜欢

记录使用fastjson消息转换器遇到的问题和解决方法

企业服务总线--MuleESB简介

PLSQL Developer 14安装详解

leetcode 59. Spiral matrix II

Lecture de l'article: mesurer l'impact d'un accès DDOS réussi sur le comportement du client du serveur DNS géré

论文阅读:Measuring the Impact of a Successful DDoS Attack on the Customer Behaviour of Managed DNS Servi

服务器部署svn环境

H + background UI framework, click the button to create a new tab

Steps for umlet beginners

Understanding of data warehouse ODS, DW and DM concepts in data governance platform
随机推荐
论文阅读:Domain Name Encryption Is Not Enough: Privacy Leakage via IP-based Website Fingerprinting
H + background UI framework, click the button to create a new tab
Signals
Panic and Recover
leetcode 27.移除元素
php Rsa加密
Understanding of data warehouse ODS, DW and DM concepts in data governance platform
论文阅读:Analyzing Third Party Service Dependencies in Modern Web Services
sqlmap post 注入方法
2021-10-17
SQLServer的3种分页查询方式sql2000/2008/2012的
php 中文转化为英文首字母
div点击折叠收缩
asp.net re正则表达式,筛选返回的字符串中时间单号金额,把不规范的时间格式转化为正确的时间格式
根据二维数组某个字段的值查找数组
leetcode 203.移除链表元素
每隔30秒对局域网电脑发送一次关机命令
Exit and Status
论文阅读:Security Challenges in an Increasingly Tangled Web
playwright,selenium,操作ifram元素