当前位置:网站首页>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)
边栏推荐
- (CVPR-2019)选择性的内核网络
- Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan
- [7.21-26] code source - [good sequence] [social circle] [namonamo]
- Data security is a competitive advantage. How can companies give priority to information security and compliance
- Leetcode 113: path sum II
- 【Golang】- runtime.Goexit()
- How to deal with the DDoS attack on the game server and how to defend it?
- Anaconda environment installation problem
- Know that Chuangyu is listed in many fields of ccsip 2022 panorama
- druid. io index_ Realtime real-time query
猜你喜欢

Why does stonedb dare to call it the only open source MySQL native HTAP database in the industry?

ASCII code table

Talk about possible problems when using transactions (@transactional)

数学建模——自来水管道铺设问题

【流放之路-第八章】

DSP vibration seat

Js DOM2 和 DOM3

Web crawler API Quick Start Guide

【Golang】- runtime.Goexit()

【golang】使用select {}
随机推荐
E-commerce keyword research helps data collection
动态内存与智能指针
[the road of Exile - Chapter 4]
【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索
[the road of Exile - Chapter III]
[WesternCTF2018]shrine
[golang] use select {}
The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
druid. io index_ Realtime real-time query
Data security is a competitive advantage. How can companies give priority to information security and compliance
How to protect WordPress website from network attack? It is essential to take safety measures
【流放之路-第六章】
正则过滤数据学习笔记(①)
Analyze OP based on autoware_ global_ Planner global path planning module re planning
Practical experience of Google cloud spanner
StoneDB 邀请您参与开源社区月会!
Explanation of yocto project directory structure
移动通信——基于卷积码的差错控制系统仿真模型
Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan