当前位置:网站首页>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;
}边栏推荐
- Codeforces 771-div2 C (trouble, permutation is not very good)
- HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
- b格高且好看的代码片段分享图片生成
- Depth filter of SvO2 series
- YYGH-9-预约下单
- YYGH-BUG-04
- Seriation in R: How to Optimally Order Objects in a Data Matrice
- 【C语言】十进制数转换成二进制数
- Develop scalable contracts based on hardhat and openzeppelin (I)
- Dynamic memory (advanced 4)
猜你喜欢

SVO2系列之深度滤波DepthFilter

Natural language processing series (II) -- building character level language model using RNN

Some problems encountered in introducing lvgl into esp32 Arduino

How to Add P-Values onto Horizontal GGPLOTS

HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R

H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开

SVO2系列之深度濾波DepthFilter

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

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

PyTorch nn.RNN 参数全解析
随机推荐
深入理解PyTorch中的nn.Embedding
Leetcode122 买卖股票的最佳时机 II
自然语言处理系列(一)——RNN基础
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
to_ Bytes and from_ Bytes simple example
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
PX4 Position_ Control RC_ Remoter import
【C语言】杨辉三角,自定义三角的行数
PHP query distance according to longitude and latitude
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
【C语言】十进制数转换成二进制数
CMake交叉编译
Analyse de l'industrie
K-Means Clustering Visualization in R: Step By Step Guide
Codeforces 771-div2 C (trouble, permutation is not very good)
(C语言)八进制转换十进制
The selected cells in Excel form have the selection effect of cross shading
全链路压测
PyTorch nn.RNN 参数全解析
动态内存(进阶四)