当前位置:网站首页>Leetcode-209-subarray with the smallest length
Leetcode-209-subarray with the smallest length
2022-04-22 21:07:00 【Lion, tiger and leopard】
Subarray with the smallest length
Title Description : 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 [nums l, numsl+1, …, numsr-1, numsr] , And return its length . If there is no sub array that meets the conditions , return 0 .
Please refer to LeetCode Official website .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/minimum-size-subarray-sum/
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Solution 1 : The sliding window
First , Judge special cases , If the array is empty , Go straight back to 0.
otherwise , Use the sliding window method to judge whether there is the smallest continuous subarray , The specific processing logic is as follows :
- First , Declare the left and right boundaries of the sliding window , And use minLen Record the length of the smallest continuous subarray ;
- Traversal array , Until the right boundary traverses to the last digit position of the original array ;
- Calculates the sum of successive subarrays of the current range , Judge whether the current sum is better than target Small , If not less than , Move the left boundary of the window to the right , And judge whether the current continuous length is the smallest ; If it is less than , Move the right boundary of the window to the right .
Last , Judge minLen Have you found , Returns if it does not exist 0; otherwise , return minLen.
public class LeetCode_209 {
/** * The sliding window * * @param target The target * @param nums Original array * @return */
public static int minSubArrayLen(int target, int[] nums) {
// If the array is empty , Go straight back to 0
if (nums == null || nums.length == 0) {
return 0;
}
// Record the length of the smallest continuous subarray
int minLen = Integer.MAX_VALUE;
// Slide the left and right boundaries of the window
int left = 0, right = 0;
// Traverse until the right boundary traverses to the last digit position of the original array
while (right < nums.length) {
int sum = 0;
// Calculates the sum of successive subarrays of the current range
for (int i = left; i <= right; i++) {
sum += nums[i];
}
// Judge whether the current sum is better than target Small , If not less than , Move the left boundary of the window to the right , And judge whether the current continuous length is the smallest ; If it is less than , Move the right boundary of the window to the right
if (sum >= target) {
minLen = Math.min(minLen, right - left + 1);
left++;
} else {
right++;
}
}
if (minLen == Integer.MAX_VALUE) {
return 0;
}
return minLen;
}
public static void main(String[] args) {
// Test case one , Expected output : 2
System.out.println(minSubArrayLen(7, new int[]{
2, 3, 1, 2, 4, 3}));
// Test case 2 , Expected output : 0
System.out.println(minSubArrayLen(11, new int[]{
1, 1, 1, 1, 1, 1, 1, 1}));
}
}
【 Daily message 】 Life is like climbing a mountain , And looking for the mountain to find the way , But it's a learning process , In the process , Study firmly 、 calm , Learn how to find life from panic .
版权声明
本文为[Lion, tiger and leopard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222101193354.html
边栏推荐
- 重返天梯-L2-025 分而治之 (25 分)
- 2022年G3锅炉水处理国家题库及在线模拟考试
- Evaluaion mark in natural language processing field//updating
- 机器视觉需要掌握的知识
- [Istio是什么?] 还不知道你就out了,一文40分钟快速理解
- 1、 Machine learning concepts
- Win10 installation neo4j
- Smart agriculture has become a development path, give full play to intelligence and liberate manpower
- 88 r user portrait linear regression logical regression comprehensive practice 1
- 容联云携手中国信通院,开启办公即时通信软件系列标准研制
猜你喜欢

LeetCode HOT 100(买卖股票的最佳时机)

Nodejs notes 4

Introduction to unityshader - sketch effect rendering

SDF accelerate trace

L1-046 divide singles

DSPACE simulates simple accident scene
![[radar] point target simulation of simulated synthetic aperture radar (SAR)](/img/65/12a9a76c5e5590e0008bdc00f3148c.png)
[radar] point target simulation of simulated synthetic aperture radar (SAR)

Introduction to dynamic programming of leetcode learning plan day1,2 (4 questions in total)

蜥蜴书学习day1-机器学习概述

LeetCode刷题日记 - 剑指 Offer II 070. 排序数组中只出现一次的数字
随机推荐
TC 结构管理器 - 打包 解包
SDF accelerate trace
Application value of smart agriculture app software
Massive mapper IO optimization (using multithreaded asynchrony + countdownlatch)
Use of dSPACE simulation platform
Selenium_ Webdriver video automation script sharing
2022年电工(初级)考试题库及在线模拟考试
Industrial control safety solution
On "waiting for awakening mechanism"
基于GD32F1x0手动编程实现简易freertos之任务阻塞延时
【数据库学习01】
DSPACE simulates simple accident scene
MySQL 进阶 存储过程 存储函数 -- 存储过程介绍、存储过程基本语法、变量(系统变量、用户定义变量、局部变量)、if、参数、case、while、repeat、loop、游标、条件处理程序
2022 question bank and online simulation examination of hoisting machinery command certificate
leetcode-92-反转链表
【youcans 的 OpenCV 例程200篇】160. 图像处理之OTSU 方法
第一章 虚拟现实技术概论
Introduction to dynamic programming of leetcode learning plan day1,2 (4 questions in total)
对Swin-T中SW-MSA的一些理解
农村没网络怎样安监控,家里没有wifi安哪种监控器