当前位置:网站首页>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;
}边栏推荐
- Fabric. JS 3 APIs to set canvas width and height
- PyTorch nn. Full analysis of RNN parameters
- Dynamic debugging of multi file program x32dbg
- 还不会安装WSL 2?看这一篇文章就够了
- 机械臂速成小指南(七):机械臂位姿的描述方法
- SVO2系列之深度滤波DepthFilter
- 深入理解PyTorch中的nn.Embedding
- How to Easily Create Barplots with Error Bars in R
- [visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
- BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
猜你喜欢

HOW TO ADD P-VALUES TO GGPLOT FACETS

(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?

【C语言】十进制数转换成二进制数

动态内存(进阶四)

PyTorch搭建LSTM实现服装分类(FashionMNIST)

Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law

Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement

Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator

Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)

HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
随机推荐
PHP query distance according to longitude and latitude
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Leetcode922 按奇偶排序数组 II
xss-labs-master靶场环境搭建与1-6关解题思路
Three transparent LED displays that were "crowded" in 2022
Log4j2
Natural language processing series (I) -- RNN Foundation
XSS labs master shooting range environment construction and 1-6 problem solving ideas
On data preprocessing in sklearn
[untitled] how to mount a hard disk in armbian
to_ Bytes and from_ Bytes simple example
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
Implementation of address book (file version)
史上最易懂的f-string教程,收藏這一篇就够了
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
Deep understanding of NN in pytorch Embedding
Some problems encountered in introducing lvgl into esp32 Arduino
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。