diff --git a/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num209.java b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num209.java new file mode 100644 index 0000000..6a16060 --- /dev/null +++ b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num209.java @@ -0,0 +1,26 @@ +package com.cool.ling_cha_mount.sliding_windows; + +/** + * Created with IntelliJ IDEA. + * + * @Author: Cool + * @Date: 2024/10/29/17:09 + * @Description: 209. 长度最小的子数组 + * + */ +public class Num209 { + public int minSubArrayLen(int target, int[] nums) { + int res = nums.length + 1; + int left = 0; + int sum = 0; + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; + while (sum >= target) { + res = Math.min(res, i - left + 1); + sum -= nums[left]; + left++; + } + } + return res > nums.length ? 0 : res; + } +} diff --git a/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2904.java b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2904.java new file mode 100644 index 0000000..bf586e8 --- /dev/null +++ b/src/main/java/com/cool/ling_cha_mount/sliding_windows/Num2904.java @@ -0,0 +1,97 @@ +package com.cool.ling_cha_mount.sliding_windows; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * + * @Author: Cool + * @Date: 2024/10/29/19:19 + * @Description: 2904. 最短且字典序最小的美丽子字符串 + */ +public class Num2904 { + public String shortestBeautifulSubstring(String s, int k) { + int left = 0; + int right = 0; + String res = ""; + char[] chars = s.toCharArray(); + while (left <= right && right < chars.length) { + if (chars[right] == '1') { + k--; + } + if (k == 0) { + while (chars[left] != '1') { + left++; + } + if (res.isEmpty()) { + res = s.substring(left, right + 1); + } else { + if (res.length() > right - left + 1) { + res = s.substring(left, right + 1); + } else if (res.length() == right - left + 1) { + String ss = s.substring(left, right + 1); + if (res.compareTo(ss) > 0) { + res = ss; + } + } + } + + left++; + k++; + } + right++; + } + return res; + } + public String shortestBeautifulSubstring1(String s, int k) { + int left = 0; + int right = 0; + List list = new ArrayList<>(); + char[] chars = s.toCharArray(); + while (left <= right && right < chars.length) { + if (chars[right] == '1') { + k--; + } + if (k == 0) { + while (chars[left] != '1') { + left++; + } + if (list.isEmpty()) { + list.add(new int[]{left, right}); + } else { + int[] arr = list.get(0); + if ((arr[1] - arr[0]) > right - left) { + list.clear(); + list.add(new int[]{left, right}); + } else if ((arr[1] - arr[0]) == right - left) { + list.add(new int[]{left, right}); + } + } + + left++; + k++; + } + right++; + } + if (list.isEmpty()) { + return ""; + } + String ans = s.substring(list.get(0)[0], list.get(0)[1] + 1); + for (int i = 1; i < list.size(); i++) { + int[] arr = list.get(i); + String ss = s.substring(arr[0], arr[1] + 1); + if (ss.compareTo(ans) < 0) { + ans = ss; + } + } + return ans; + } + + @Test + public void test() { + shortestBeautifulSubstring("100011001", 3); + } +}