当前位置:网站首页>Leetcode209 subarray with the smallest length
Leetcode209 subarray with the smallest length
2022-07-02 12:05:00 【Monsters 114】
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 .
The sliding window :
Define two pointers start and end Each represents a subarray ( Sliding window ) Start and end positions of , Maintenance variables sum Stores the elements and in the subarray nums[start] To nums[end] Elements and .
In the initial state ,start and end All point to subscripts 0,sum The value of is 0.
Every iteration , take nums[end] Add to sum, If sum≥s, Then update the minimum length of the subarray ( At this point, the length of the subarray is end−start+1), And then nums[start] from sum Subtract from and start Move right , until sum<s, In this process, the minimum length of the subarray is also updated . At the end of each iteration , take end Move right .
public int minSubArrayLen(int target, int[] nums) {
int start = 0;
int len = Integer.MAX_VALUE;
int sum = 0;
for(int end = 0; end < nums.length; end++){
sum += nums[right];
while(sum >= target){
len = Math.min(len,end-start+1);
sum -= nums[start++];
}
}
return len == Integer.MAX_VALUE ? 0 : len;
}
边栏推荐
- Leetcode922 按奇偶排序数组 II
- Leetcode209 长度最小的子数组
- 【工控老马】西门子PLC Siemens PLC TCP协议详解
- YYGH-10-微信支付
- xss-labs-master靶场环境搭建与1-6关解题思路
- to_ Bytes and from_ Bytes simple example
- 史上最易懂的f-string教程,收藏這一篇就够了
- Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
- Log4j2
- Log4j2
猜你喜欢
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
HOW TO ADD P-VALUES TO GGPLOT FACETS
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
深入理解PyTorch中的nn.Embedding
YYGH-BUG-05
K-Means Clustering Visualization in R: Step By Step Guide
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
Yygh-9-make an appointment to place an order
自然语言处理系列(三)——LSTM
随机推荐
How to Easily Create Barplots with Error Bars in R
File operation (detailed!)
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
MySQL stored procedure cursor traversal result set
xss-labs-master靶场环境搭建与1-6关解题思路
to_bytes与from_bytes简单示例
Applet link generation
Filtre de profondeur de la série svo2
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
The position of the first underline selected by the vant tabs component is abnormal
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
K-Means Clustering Visualization in R: Step By Step Guide
YYGH-9-预约下单
MSI announced that its motherboard products will cancel all paper accessories
多文件程序X32dbg动态调试
Mish-撼动深度学习ReLU激活函数的新继任者
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
Those logs in MySQL
How to Add P-Values onto Horizontal GGPLOTS