当前位置:网站首页>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;
}边栏推荐
- Log4j2
- Jenkins用户权限管理
- SVO2系列之深度濾波DepthFilter
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- 浅谈sklearn中的数据预处理
- Data analysis - Matplotlib sample code
- pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
- Depth filter of SvO2 series
- [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
- Flesh-dect (media 2021) -- a viewpoint of material decomposition
猜你喜欢

GGPlot Examples Best Reference

Esp32 audio frame esp-adf add key peripheral process code tracking

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

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

Natural language processing series (I) -- RNN Foundation

Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)

机械臂速成小指南(七):机械臂位姿的描述方法

YYGH-BUG-05

测试左移和右移

How to Create a Beautiful Plots in R with Summary Statistics Labels
随机推荐
【2022 ACTF-wp】
YYGH-BUG-05
xss-labs-master靶场环境搭建与1-6关解题思路
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Power Spectral Density Estimates Using FFT---MATLAB
Filtre de profondeur de la série svo2
php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
Leetcode122 买卖股票的最佳时机 II
K-Means Clustering Visualization in R: Step By Step Guide
How to Visualize Missing Data in R using a Heatmap
Seriation in R: How to Optimally Order Objects in a Data Matrice
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Esp32 audio frame esp-adf add key peripheral process code tracking
PyTorch nn. Full analysis of RNN parameters
Leetcode739 每日温度
How to Easily Create Barplots with Error Bars in R
YYGH-10-微信支付
to_ Bytes and from_ Bytes simple example
[visual studio 2019] create and import cmake project
倍增 LCA(最近公共祖先)