当前位置:网站首页>leetcode209. Minimum length subarray
leetcode209. Minimum length subarray
2022-07-19 04:26:00 【pearz】
Address :https://leetcode.cn/problems/minimum-size-subarray-sum/
describe : 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 [ n u m s l nums_l numsl, n u m s l + 1 nums_{l+1} numsl+1, …, n u m s r − 1 nums_{r-1} numsr−1, n u m s r nums_r numsr] , And return its length . If there is no sub array that meets the conditions , return 0 .
example 1:
Input :target = 7, nums = [2,3,1,2,4,3]
Output :2
explain : Subarray [4,3] Is the smallest subarray in this condition .
example 2:
Input :target = 4, nums = [1,4,4]
Output :1
example 3:
Input :target = 11, nums = [1,1,1,1,1,1,1,1]
Output :0
Ideas : The sliding window
answer :
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int len = nums.length;
int left = 0;
int sum = 0;
int res = Integer.MAX_VALUE;
for (int right = 0; right < len; right++) {
sum += nums[right];
while (sum >= target) {
res = Math.min(res, right - left + 1);
sum -= nums[left++];
}
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}
边栏推荐
猜你喜欢

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(3)后台功能

06 Maui, WPF uses MVVM toolkit framework to build MVVM program

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(2)小程序功能

Technical writing guide for programmers to leave work early

Chapter 5 performance platform godeye source code analysis - third party module

By voting for the destruction of STI by Dao, seektiger is truly community driven

Mqant in-depth analysis

如何更有效的过滤病毒/垃圾邮件!

ASP. Net1==visual studio create asp net demo

Machine learning 11: cost sensitive learning
随机推荐
论文研究NLP
Cabasicanimation pause \ enterprise action
OSPF基础优化
STM32 - timer series (II) general timer
V4L2学习资料收集
C# 构造函数(Constructors)简单讲解
64. 最小路径和:给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。
Wechat online education video on demand learning applet graduation design (1) development outline
Leetcode7 DFS + dynamic programming + double pointer
C # explain out output parameters in detail
[database] must know and know at the end of the period ----- Chapter 12 database recovery
Android kotlin自定义自动换行LinearLayout
B+树存储过程、触发器、Substring和substr的区别及Truncate和Delete的区别
【微信小程序】超易懂的条件渲染和列表渲染
Intel helps open medical service and promote the intellectualization of ultrasonic prenatal examination
Chapter 5 performance platform godeye source code analysis - third party module
Machine learning 11: cost sensitive learning
Set administrator permissions for idea and console
小程序畢設作品之微信在線教育視頻點播學習小程序畢業設計(3)後臺功能
Introduction to Maui framework 05 MVVM data model understanding