Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions L2023110961_18_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import org.junit.Test;
import static org.junit.Assert.*;

/*
* 测试用例设计总体原则:
* 1. 等价类划分:正数、负数、包含0、单元素、多元素
* 2. 边界值分析:数组长度边界(最小2个元素)、数值边界
* 3. 特殊情况:包含0的数组、全负数、全正数、混合正负数
*/
public class L2023110961_18_Test {

/**
* 测试目的:验证基本功能 - 正整数数组
* 测试用例:[1,2,3,4]
* 预期输出:[24,12,8,6]
* 等价类:正整数数组
*/
@Test
public void testBasicPositiveNumbers() {
Solution solution = new Solution();
int[] nums = {1, 2, 3, 4};
int[] expected = {24, 12, 8, 6};
assertArrayEquals(expected, solution.productExceptSelf(nums));
}

/**
* 测试目的:验证包含0的情况
* 测试用例:[-1,1,0,-3,3]
* 预期输出:[0,0,9,0,0]
* 等价类:包含0的混合数组
*/
@Test
public void testArrayWithZero() {
Solution solution = new Solution();
int[] nums = {-1, 1, 0, -3, 3};
int[] expected = {0, 0, 9, 0, 0};
assertArrayEquals(expected, solution.productExceptSelf(nums));
}

/**
* 测试目的:验证边界条件 - 最小长度数组(2个元素)
* 测试用例:[2,3]
* 预期输出:[3,2]
* 边界值:最小长度
*/
@Test
public void testMinimumLength() {
Solution solution = new Solution();
int[] nums = {2, 3};
int[] expected = {3, 2};
assertArrayEquals(expected, solution.productExceptSelf(nums));
}

/**
* 测试目的:验证负数处理
* 测试用例:[-2,-3,-4]
* 预期输出:[12,8,6]
* 等价类:全负数数组
*/
@Test
public void testAllNegativeNumbers() {
Solution solution = new Solution();
int[] nums = {-2, -3, -4};
int[] expected = {12, 8, 6};
assertArrayEquals(expected, solution.productExceptSelf(nums));
}

/**
* 测试目的:验证包含1的情况
* 测试用例:[1,1,1,1]
* 预期输出:[1,1,1,1]
* 特殊情况:全1数组
*/
@Test
public void testAllOnes() {
Solution solution = new Solution();
int[] nums = {1, 1, 1, 1};
int[] expected = {1, 1, 1, 1};
assertArrayEquals(expected, solution.productExceptSelf(nums));
}

/**
* 测试目的:验证包含多个0的情况
* 测试用例:[0,0,1]
* 预期输出:[0,0,0]
* 特殊情况:多个0
*/
@Test
public void testMultipleZeros() {
Solution solution = new Solution();
int[] nums = {0, 0, 1};
int[] expected = {0, 0, 0};
assertArrayEquals(expected, solution.productExceptSelf(nums));
}

/**
* 测试目的:验证较大数组
* 测试用例:[2,3,4,5]
* 预期输出:[60,40,30,24]
* 等价类:正整数数组(较长)
*/
@Test
public void testLargerArray() {
Solution solution = new Solution();
int[] nums = {2, 3, 4, 5};
int[] expected = {60, 40, 30, 24};
assertArrayEquals(expected, solution.productExceptSelf(nums));
}
}
7 changes: 4 additions & 3 deletions Solution18.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
* 输入: nums = [-1,1,0,-3,3]
* 输出: [0,0,9,0,0]
*/

class Solution {
public int[] productExceptSelf(int[] nums) {
int length === nums.length;
int length = nums.length;

// L 和 R 分别表示左右两侧的乘积列表
int[] L = new int[length];
Expand All @@ -29,7 +30,7 @@ public int[] productExceptSelf(int[] nums) {
// 对于索引为 '0' 的元素,因为左侧没有元素,所以 L[0] = 1
L[0] = 1;
for (int i = 1; i < length; i++) {
L(i) = nums[i - 1] * L[i - 1];
L[i] = nums[i - 1] * L[i - 1];
}

// R[i] 为索引 i 右侧所有元素的乘积
Expand All @@ -40,7 +41,7 @@ public int[] productExceptSelf(int[] nums) {
}

// 对于索引 i,除 nums[i] 之外其余各元素的乘积就是左侧所有元素的乘积乘以右侧所有元素的乘积
for {int i = 0; i < length; i++} {
for (int i = 0; i < length; i++) {
answer[i] = L[i] * R[i];
}

Expand Down