From 5926432b0ace0537b6dabaa154a48b44b0aae06d Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 10 Dec 2023 21:09:11 +0800 Subject: [PATCH] =?UTF-8?q?LeetCode209=20=E5=8F=8C=E6=8C=87=E9=92=88?= =?UTF-8?q?=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3=20while=E4=B8=8D=E6=96=AD?= =?UTF-8?q?=E5=8E=BB=E9=99=A4sum=E4=B8=AD=E7=9A=84=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E7=9B=B4=E5=88=B0=E4=B8=8D=E6=BB=A1=E8=B6=B3>target=E7=9A=84?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/middle/LeetCode209.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/middle/LeetCode209.java diff --git a/src/main/java/cn/whaifree/leetCode/middle/LeetCode209.java b/src/main/java/cn/whaifree/leetCode/middle/LeetCode209.java new file mode 100644 index 0000000..838a30f --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/middle/LeetCode209.java @@ -0,0 +1,90 @@ +package cn.whaifree.leetCode.middle; + +import org.junit.Test; + +/** + * 209. 长度最小的子数组 + * 给定一个含有 n 个正整数的数组和一个正整数 target 。 + * + * 找出该数组中满足其总和大于等于 target 的长度最小的 连续子数组 [numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。 + * + * 示例 1: + * + * 输入:target = 7, nums = [2,3,1,2,4,3] + * 输出:2 + * 解释:子数组 [4,3] 是该条件下的长度最小的子数组。 + * 示例 2: + * + * 输入:target = 4, nums = [1,4,4] + * 输出:1 + * 示例 3: + * + * 输入:target = 11, nums = [1,1,1,1,1,1,1,1] + * 输出:0 + * + * 提示: + * + * 1 <= target <= 109 + * 1 <= nums.length <= 105 + * 1 <= nums[i] <= 105 + * + * 进阶: + * + * 如果你已经实现 O(n) 时间复杂度的解法, 请尝试设计一个 O(n log(n)) 时间复杂度的解法。 + */ +public class LeetCode209 { + + /** + * 暴力求解,会超时 + * @param target + * @param nums + * @return + */ + public int minSubArrayLen(int target, int[] nums) { + + int minLength = Integer.MAX_VALUE; + for (int i = 0; i < nums.length; i++) { + int sum = 0; + for (int j = i; j < nums.length; j++) { + sum += nums[j]; + if (sum >= target) { + minLength = Math.min(minLength, j - i + 1); + break; + } + /** + * if (sum >= target && j - i +1 < minLength) { + * minLength = j - i + 1; + * break; + * } + */ + } + } + + + return minLength == Integer.MAX_VALUE ? 0 : minLength; + } + + public int minSubArrayLen1(int target, int[] nums) { + int left = 0; + int right = 0; + int sum = 0; + int ans = Integer.MAX_VALUE; + while (right < nums.length ) { + sum += nums[right]; + // 窗口内,找到最小子串 + while (sum >= target) { + ans = Math.min(ans, right - left + 1); + sum -= nums[left++]; + } + right++; + } + return ans == Integer.MAX_VALUE ? 0 : ans; + } + + + @Test + public void list() { + System.out.println(minSubArrayLen1(5, new int[]{2,3,6})); + } + +}