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
Binary file added L2023111998_4_Test.class
Binary file not shown.
113 changes: 113 additions & 0 deletions L2023111998_4_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import org.junit.Test;
import static org.junit.Assert.*;

/**
* 测试类:L2023111998_4_Test
* 测试目标:Solution4.maximumGap(int[] nums)
*
* 测试用例设计原则:等价类划分法
* 根据题目要求和算法特性,划分以下等价类:
* 1. 边界情况等价类:数组长度小于2(空数组、单元素数组)
* 2. 正常情况等价类:已排序数组、未排序数组
* 3. 特殊情况等价类:所有元素相同、包含大数值、负数情况
* 4. 典型示例等价类:题目给出的标准示例
*/

public class L2023111998_4_Test {

/**
* 测试目的:验证数组长度小于2时返回0
* 测试用例:单元素数组 [10]
* 等价类:边界情况等价类
*/
@Test
public void testSingleElement() {
Solution4 solution = new Solution4();
int[] nums = {10};
assertEquals("单元素数组应返回0", 0, solution.maximumGap(nums));
}

/**
* 测试目的:验证空数组返回0
* 测试用例:空数组 []
* 等价类:边界情况等价类
*/
@Test
public void testEmptyArray() {
Solution4 solution = new Solution4();
int[] nums = {};
assertEquals("空数组应返回0", 0, solution.maximumGap(nums));
}

/**
* 测试目的:验证已排序数组能正确计算最大间隙
* 测试用例:已排序数组 [1, 3, 6, 9]
* 等价类:正常情况等价类(已排序)
*/
@Test
public void testSortedArray() {
int[] nums = {1, 3, 6, 9};
Solution4 solution = new Solution4();
assertEquals("已排序数组应正确计算最大间隙", 3, solution.maximumGap(nums));
}

/**
* 测试目的:验证未排序数组排序后能正确计算最大间隙
* 测试用例:未排序数组 [3, 6, 9, 1]
* 等价类:正常情况等价类(未排序)
*/
@Test
public void testUnsortedArray() {
int[] nums = {3, 6, 9, 1};
Solution4 solution = new Solution4();
assertEquals("未排序数组排序后应正确计算最大间隙", 3, solution.maximumGap(nums));
}

/**
* 测试目的:验证所有元素相同时最大间隙为0
* 测试用例:相同元素数组 [5, 5, 5, 5]
* 等价类:特殊情况等价类
*/
@Test
public void testAllSameElements() {
int[] nums = {5, 5, 5, 5};
Solution4 solution = new Solution4();
assertEquals("所有元素相同时最大间隙应为0", 0, solution.maximumGap(nums));
}

/**
* 测试目的:验证算法处理大数值的能力
* 测试用例:大数值数组 [1000000, 1, 10000000, 100]
* 等价类:特殊情况等价类(大数值)
*/
@Test
public void testLargeNumbers() {
int[] nums = {1000000, 1, 10000000, 100};
Solution4 solution = new Solution4();
assertEquals("应正确处理大数值数组", 9000000, solution.maximumGap(nums));
}

/**
* 测试目的:验证题目示例1的正确性
* 测试用例:示例数组 [3, 6, 9, 1]
* 等价类:典型示例等价类
*/
@Test
public void testExample1() {
int[] nums = {3, 6, 9, 1};
Solution4 solution = new Solution4();
assertEquals("示例1应返回3", 3, solution.maximumGap(nums));
}

/**
* 测试目的:验证题目示例2的正确性
* 测试用例:单元素数组 [10]
* 等价类:典型示例等价类
*/
@Test
public void testExample2() {
int[] nums = {10};
Solution4 solution = new Solution4();
assertEquals("示例2应返回0", 0, solution.maximumGap(nums));
}
}
Binary file added Solution4.class
Binary file not shown.
55 changes: 14 additions & 41 deletions Solution4.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,36 @@
import java.util.Arrays;

/**
* @description:
*
* 给定一个无序的数组 nums,返回 数组在排序之后,相邻元素之间最大的差值 。如果数组元素个数小于 2,则返回 0 。
*
* 您必须编写一个在「线性时间」内运行并使用「线性额外空间」的算法。
*
*
*
* 示例 1:
*
* 输入: nums = [3,6,9,1]
* 输出: 3
* 解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。
* 示例 2:
*
* 输入: nums = [10]
* 输出: 0
* 解释: 数组元素个数小于 2,因此返回 0。
*
*
* 提示:
*
* 1 <= nums.length <= 105
* 0 <= nums[i] <= 109
*
*/
class Solution4 {
public int maximumGap(int[] nums) {

int n = nums.length - 1;
if (n < 2) {
if (nums.length < 2) {
return 0;
}
long exp = 1;
int[] buf = new int[n];
int[] buf = new int[nums.length];
int maxVal = Arrays.stream(nums).max().getAsInt();

while (maxVal > exp) {
while (maxVal >= exp) {
int[] cnt = new int[10];
for (int i = 0; i < n; i++) {
int digit = (nums[i] / (int) exp) % 10;
for (int i = 0; i < nums.length; i++) {
int digit = (int) ((nums[i] / exp) % 10);
cnt[digit]++;
}
for (int i = 1; i < 10; i++){
for (int i = 1; i < 10; i++) {
cnt[i] += cnt[i - 1];
for (int i = n - 1; i >= 0; i--) {
int digit = (nums[i] / (int) exp) % 10;
}
for (int i = nums.length - 1; i >= 0; i--) {
int digit = (int) ((nums[i] / exp) % 10);
buf[cnt[digit] - 1] = nums[i];
cnt[digit]--;
}
System.arraycopy(buf, 0, nums, 0, n);
exp += 10;
System.arraycopy(buf, 0, nums, 0, nums.length);
exp *= 10;
}

int ret = 0;
for (int i = 1; i < n; i++) {
for (int i = 1; i < nums.length; i++) {
ret = Math.max(ret, nums[i] - nums[i - 1]);
}return ret;
}
return ret;
}
}
Binary file added hamcrest-core-1.3.jar
Binary file not shown.
Binary file added junit-4.13.2.jar
Binary file not shown.