Pārlūkot izejas kodu

#feat:leetcode p1456题题解

yangyi 2 nedēļas atpakaļ
vecāks
revīzija
b395689f9f
2 mainītis faili ar 111 papildinājumiem un 0 dzēšanām
  1. 66 0
      src/leetcode/p1456/Solution.java
  2. 45 0
      src/leetcode/p1456/SolutionTest.java

+ 66 - 0
src/leetcode/p1456/Solution.java

@@ -0,0 +1,66 @@
+package leetcode.p1456;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: Solution
+ * @Author: 杨逸
+ * @Data:2026/3/8 15:35
+ * @Description: https://leetcode.cn/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/
+ * 1456. 定长子串中元音的最大数目
+ */
+public class Solution {
+    public int maxVowels(String s, int k) {
+        //定长滑动窗口解法
+        //关注进入窗口的字符和离开窗口的字符
+        Set<Character> set = new HashSet<>();
+        set.add('a');
+        set.add('e');
+        set.add('i');
+        set.add('o');
+        set.add('u');
+        int count = 0;
+        int ans = 0;
+        for (int i = 0; i < s.length(); i++) {
+            if (i >= k) {
+                //离开窗口的字符
+                if (set.contains(s.charAt(i - k))) count--;
+            }
+            //进入窗口的字符
+            if (set.contains(s.charAt(i))) count++;
+            ans = Math.max(ans, count);
+        }
+        return ans;
+    }
+
+    /**
+     * 前缀和,计算最大期间差
+     * 前缀和统计当前长度已经出现多少个元音字符
+     * 以k为长度的滑动窗口,统计窗口内元音字符的个数
+     * @param s
+     * @param k
+     * @return
+     */
+    private int prefixSolution(String s, int k){
+        //前缀和
+        int[] prefix = new int[s.length()+1];
+        int count = 0;
+        int max = 0;
+        Set<Character> set = new HashSet<>();
+        set.add('a');
+        set.add('e');
+        set.add('i');
+        set.add('o');
+        set.add('u');
+        for (int i = 0; i < s.length(); i++) {
+            if (set.contains(s.charAt(i)))count++;
+            prefix[i+1] = count;
+        }
+        for (int i = k; i < prefix.length; i++) {
+            if (prefix[i]- prefix[i-k] > max)max = prefix[i] - prefix[i-k];
+        }
+        return max;
+    }
+}

+ 45 - 0
src/leetcode/p1456/SolutionTest.java

@@ -0,0 +1,45 @@
+package leetcode.p1456;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: SolutionTest
+ * @Author: 杨逸
+ * @Data:2026/3/8 15:42
+ * @Description:
+ */
+@RunWith(Parameterized.class)
+public class SolutionTest {
+    public static final Solution solution = new Solution();
+    private String s;
+    private int k;
+    private int expected;
+
+    public SolutionTest(String s, int k, int expected) {
+        this.s = s;
+        this.k = k;
+        this.expected = expected;
+    }
+    @Parameterized.Parameters
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][]{
+                {"abciiidef",3,3},
+                {"aeiou",2,2},
+                {"leetcode",3,2},
+        });
+    }
+
+    @Test
+    public void maxVowels() {
+        int result = solution.maxVowels(s, k);
+        assertEquals(expected, result);
+    }
+}