当前位置:网站首页>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;
}边栏推荐
- Seriation in R: How to Optimally Order Objects in a Data Matrice
- 多文件程序X32dbg动态调试
- Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
- GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
- QT meter custom control
- 自然语言处理系列(三)——LSTM
- Natural language processing series (III) -- LSTM
- 基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- PX4 Position_ Control RC_ Remoter import
猜你喜欢

Flesh-dect (media 2021) -- a viewpoint of material decomposition

Larvel modify table fields

Take you ten days to easily finish the finale of go micro services (distributed transactions)

H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser

PyTorch nn.RNN 参数全解析

动态内存(进阶四)

基于Arduino和ESP8266的连接手机热点实验(成功)

xss-labs-master靶场环境搭建与1-6关解题思路

YYGH-BUG-05

YYGH-BUG-04
随机推荐
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
GGPlot Examples Best Reference
How to Easily Create Barplots with Error Bars in R
CONDA common command summary
Codeforces 771 div2 B (no one FST, refers to himself)
Larvel modify table fields
文件操作(详解!)
Power Spectral Density Estimates Using FFT---MATLAB
Log4j2
K-Means Clustering Visualization in R: Step By Step Guide
【工控老马】西门子PLC Siemens PLC TCP协议详解
时间格式化显示
Leetcode122 买卖股票的最佳时机 II
HOW TO ADD P-VALUES TO GGPLOT FACETS
PHP 2D and multidimensional arrays are out of order, PHP_ PHP scrambles a simple example of a two-dimensional array and a multi-dimensional array. The shuffle function in PHP can only scramble one-dim
行业的分析
SVO2系列之深度滤波DepthFilter
数据分析 - matplotlib示例代码
Fabric.js 3个api设置画布宽高