From 2b65c229b5d355ac736d52c53a0dc95a2153fe09 Mon Sep 17 00:00:00 2001 From: Joshua <47053655+Joshua-Lu@users.noreply.github.com> Date: Thu, 13 May 2021 22:45:20 +0800 Subject: [PATCH] =?UTF-8?q?Update=200209.=E9=95=BF=E5=BA=A6=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0209.长度最小的子数组 Java版本 --- problems/0209.长度最小的子数组.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 1b00c4e3..c4b14d6e 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -148,7 +148,24 @@ class Solution: Java: - +```java +class Solution { + // 滑动窗口 + public int minSubArrayLen(int s, int[] nums) { + int left = 0; + int sum = 0; + int result = Integer.MAX_VALUE; + for (int right = 0; right < nums.length; right++) { + sum += nums[right]; + while (sum >= s) { + result = Math.min(result, right - left + 1); + sum -= nums[left++]; + } + } + return result == Integer.MAX_VALUE ? 0 : result; + } +} +``` Python: