当前位置:网站首页>Leetcode/ and continuous shortest subarray greater than or equal to target
Leetcode/ and continuous shortest subarray greater than or equal to target
2022-07-29 02:03:00 【xcrj】
Code
package com.xcrj;
/** * The finger of the sword Offer II 008. And greater than or equal to target Continuous shortest subarray of * 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 . * Elements and >=target&& The subsequence of elements is continuous $$ The length of subsequence is the shortest */
public class Solution8 {
/** * The sliding window ( Double pointer ) * !!! Use different pointers to achieve different purposes * i j It all points to 0 * Move first j Seek sum>=target * Move again i Seek sum<target * Under the resultant force of the two phases, the subsequence with the smallest length satisfying the condition is obtained */
public int minSubArrayLen(int target, int[] nums) {
int i = 0;
int j = 0;
int sum = 0;
int minLen = Integer.MAX_VALUE;
//
while (j < nums.length) {
sum += nums[j];
while (sum >= target) {
minLen = Math.min(minLen, j - i + 1);
sum -= nums[i];
// Move i Seek sum<target
i++;
}
// Move j Seek sum>=target
j++;
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}
public static void main(String[] args) {
Solution8 solution8 = new Solution8();
System.out.println(solution8.minSubArrayLen(7, new int[]{
2, 3, 1, 2, 4, 3}));
}
}
Reference resources
author :LeetCode-Solution
link :https://leetcode.cn/problems/2VG8Kg/solution/he-da-yu-deng-yu-target-de-zui-duan-zi-s-ixef/
source : Power button (LeetCode)
边栏推荐
- druid. io index_ Realtime real-time query
- (arxiv-2018) 重新审视基于视频的 Person ReID 的时间建模
- 规划数学期末考试模拟二
- Analyze OP based on autoware_ global_ Planner global path planning module re planning
- The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
- 【流放之路-第二章】
- The basic concept of transaction and the implementation principle of MySQL transaction
- Sigma-DSP-OUTPUT
- 数学建模——仓内拣货优化问题
- Internship: tool class writing for type judgment
猜你喜欢

What is browser fingerprint recognition

Top network security prediction: nearly one-third of countries will regulate blackmail software response within three years

Nine days later, we are together to focus on the new development of audio and video and mystery technology

【流放之路-第七章】

The basic concept of transaction and the implementation principle of MySQL transaction

Where will Jinan win in hosting the first computing power conference?

druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11

为什么 BI 软件都搞不定关联分析

MPEG音频编码三十年

(CVPR-2019)选择性的内核网络
随机推荐
The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
Know that Chuangyu is listed in many fields of ccsip 2022 panorama
【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索
5g commercial third year: driverless "going up the mountain" and "going to the sea"
ASCII code table
[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
MPEG音频编码三十年
druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11
How to prevent all kinds of affiliated fraud?
Wonderful use of data analysis
Regular filtering data learning notes (①)
数学建模——公交调度优化
[7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]
[7.21-26] code source - [good sequence] [social circle] [namonamo]
Leetcode 112: path sum
Js DOM2 和 DOM3
golang启动报错【已解决】
正则过滤数据学习笔记(①)
Comprehensive analysis of news capture doorway
【7.27】代码源 - 【删数】【括号序列】【数字替换】【游戏】【画画】