Jelajahi Sumber

#feat:leetcode p238题题解

yangyi 2 minggu lalu
induk
melakukan
901fc73bbf
2 mengubah file dengan 101 tambahan dan 0 penghapusan
  1. 60 0
      src/leetcode/p238/Solution.java
  2. 41 0
      src/leetcode/p238/SolutionTest.java

+ 60 - 0
src/leetcode/p238/Solution.java

@@ -0,0 +1,60 @@
+package leetcode.p238;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: Solution
+ * @Author: 杨逸
+ * @Data:2026/3/24 19:00
+ * @Description: https://leetcode.cn/problems/product-of-array-except-self/description/
+ * 238. 除了自身以外数组的乘积
+ */
+public class Solution {
+    public int[] productExceptSelf(int[] nums) {
+        /**
+         * 前后缀分解
+         * ans[i] = 前缀[i] * 后缀[i]
+         * i前面的积乘i后面的积就是答案
+         */
+        int[] ans = new int[nums.length];
+        //calculate prefix
+        int val = 1;
+        for (int i = 0; i < nums.length; i++) {
+            ans[i] = val;
+            val *= nums[i];
+        }
+        val = 1;
+        for (int j = nums.length-1; j >= 0; j--) {
+            //space optimization;Calculate the suffix and the answer simultaneously.
+            //calculate ans
+            ans[j] = ans[j] * val;
+            //calculate suffix
+            val *= nums[j];
+        }
+        return ans;
+    }
+    private int[] prefixAndSuffixSolution(int[] nums){
+        /**
+         * 前后缀分解
+         * ans[i] = 前缀[i] * 后缀[i]
+         * i前面的积乘i后面的积就是答案
+         */
+        int[] ans = new int[nums.length];
+        int[] prefix = new int[nums.length];
+        int[] suffix = new int[nums.length];
+        //calculate prefix
+        int val = 1;
+        for (int i = 0; i < nums.length; i++) {
+            prefix[i] = val;
+            val *= nums[i];
+        }
+        //calculate suffix
+        val = 1;
+        for (int j = nums.length-1; j >= 0; j--) {
+            suffix[j] = val;
+            val *= nums[j];
+            //calculate ans
+            ans[j] = prefix[j] * suffix[j];
+        }
+        return ans;
+    }
+}

+ 41 - 0
src/leetcode/p238/SolutionTest.java

@@ -0,0 +1,41 @@
+package leetcode.p238;
+
+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.assertArrayEquals;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: SolutionTest
+ * @Author: 杨逸
+ * @Data:2026/3/24 19:03
+ * @Description:
+ */
+@RunWith(Parameterized.class)
+public class SolutionTest {
+    public static final Solution solution = new Solution();
+    private final int[] nums;
+    private final int[] expected;
+
+    public SolutionTest(int[] nums, int[] expected) {
+        this.nums = nums;
+        this.expected = expected;
+    }
+    @Parameterized.Parameters
+    public static Collection<Object[]> data(){
+        return Arrays.asList(new Object[][]{
+                {new int[]{1,2,3,4},new int[]{24,12,8,6}},
+                {new int[]{-1,1,0,-3,3},new int[]{0,0,9,0,0}},
+        });
+    }
+
+    @Test
+    public void productExceptSelf() {
+        assertArrayEquals(expected,solution.productExceptSelf(nums));
+    }
+}