Răsfoiți Sursa

#feat:leetcode p41题题解

yangyi 2 săptămâni în urmă
părinte
comite
bede39a62c
2 a modificat fișierele cu 119 adăugiri și 0 ștergeri
  1. 71 0
      src/leetcode/p41/Solution.java
  2. 48 0
      src/leetcode/p41/SolutionTest.java

+ 71 - 0
src/leetcode/p41/Solution.java

@@ -0,0 +1,71 @@
+package leetcode.p41;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: Solution
+ * @Author: 杨逸
+ * @Data:2026/3/6 22:01
+ * @Description: https://leetcode.cn/problems/first-missing-positive/description/
+ * 41. 缺失的第一个正数
+ */
+public class Solution {
+    public int firstMissingPositive(int[] nums) {
+        /**
+         * 找缺失的第一个正整数
+         * 设定数字1应该储存在nums[0]中,数字2应该储存在nums[1]中,以此类推
+         * 假设[0,index]为以调整好的部分
+         * 从nums[index]开始调整,将数字放在符合设定的位置,负数则直接跳过,
+         */
+        int index = 0;
+        while (index < nums.length){
+            if (nums[index] > 0 && nums[index] <= nums.length){
+                int temp = nums[index];
+                //如果nums[index]已经在正确的位置上,则跳过
+                if (nums[index] == nums[temp-1]){
+                    index++;
+                    continue;
+                }
+                //将nums[index]放到符合设定的位置
+                nums[index] = nums[temp-1];
+                nums[temp-1] = temp;
+            }else{
+                index++;
+            }
+        }
+        for (int i = 0; i < nums.length; i++) {
+            if (nums[i] != i+1)
+                return i+1;
+        }
+        return nums.length+1;
+    }
+
+    /**
+     * 用哈希表统计,然后从1开始判断是否存在哈希表中
+     * @param nums
+     * @return int
+     * @description:
+     * @author: 杨逸
+     * @data:2026/03/06 22:04:15
+     * @since 1.0.0
+     */
+    private int hashMapSolution(int[] nums){
+        //将所有元素加入哈希表,然后从1开始判断是否存在哈希表中
+        Set<Integer> set = new HashSet<>();
+        for (int num : nums) {
+            if (num > 0 && !set.contains(num)){
+                set.add(num);
+            }
+        }
+        int index = 1;
+        while(index < Integer.MAX_VALUE){
+            if (!set.contains(index)){
+                return index;
+            }
+            index++;
+        }
+        return 1;
+    }
+}

+ 48 - 0
src/leetcode/p41/SolutionTest.java

@@ -0,0 +1,48 @@
+package leetcode.p41;
+
+
+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/6 22:16
+ * @Description:
+ */
+@RunWith(Parameterized.class)
+public class SolutionTest {
+    private static final Solution solution = new Solution();
+    //1.参数载体
+    private int[] nums;
+    private int expected;
+
+    // 2. 构造函数:JUnit 4 会自动将 @Parameters 中的每一组数据注入
+    public SolutionTest(int[] nums, int expected) {
+        this.nums = nums;
+        this.expected = expected;
+    }
+    //3.提供数据
+    @Parameterized.Parameters(name = "{index}: input={0}, expected={1}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+                { new int[]{1, 2, 0}, 3 },          // 基础情况
+                { new int[]{3, 4, -1, 1}, 2 },      // 含负数
+                { new int[]{7, 8, 9, 11, 12}, 1 },  // 缺失1
+        });
+    }
+
+    @Test
+    public void firstMissingPositive() {
+        int result = solution.firstMissingPositive(nums);
+        assertEquals("输入 " + Arrays.toString(nums) + " 时结果错误",
+                expected, result);
+    }
+}