Compare commits
4 Commits
feb1f5fcad
...
0295bde18c
| Author | SHA1 | Date |
|---|---|---|
|
|
0295bde18c | |
|
|
4b6f4dc631 | |
|
|
6ca0b464a8 | |
|
|
a3f5a177a5 |
|
|
@ -91,6 +91,7 @@ class BinarySearch {
|
|||
return candidate;
|
||||
}
|
||||
|
||||
//改进二分查找
|
||||
public static int binarySearchLeftMost0(int[] nums, int target) {
|
||||
int i = 0, j = nums.length - 1;
|
||||
while (i <= j) {
|
||||
|
|
@ -116,4 +117,25 @@ class BinarySearch {
|
|||
}
|
||||
return i - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置。
|
||||
* 如果数组中不存在目标值 target,返回 [-1, -1]。
|
||||
* 你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。
|
||||
*
|
||||
* @param nums
|
||||
* @param target
|
||||
* @return
|
||||
*/
|
||||
public static int[] searchRange(int[] nums, int target) {
|
||||
if (nums.length == 0){
|
||||
return new int[]{-1,-1};
|
||||
}
|
||||
if (nums.length == 1){
|
||||
return new int[]{0,0};
|
||||
}
|
||||
int left = binarySearchLeftMost(nums, target);
|
||||
int right = binarySearchRightMost(nums,target);
|
||||
return new int[]{left,right};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
package com.nbee.solution.practice;
|
||||
|
||||
public class DynamicArray {
|
||||
private int size = 0;
|
||||
private int capacity = 8;
|
||||
private int[] array = new int[capacity];
|
||||
|
||||
public void addList(int element) {
|
||||
add(size, element);
|
||||
}
|
||||
|
||||
public void add(int index, int element) {
|
||||
if (index < 0 || index > size){
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
if (index >= 0 && index < size) {
|
||||
System.arraycopy(array, index, array, index + 1, size - index);
|
||||
}
|
||||
array[index] = element;
|
||||
size++;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
public int get(int index) {
|
||||
if (index < 0 || index >= size){
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
return array[index];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -97,18 +97,18 @@ public class BinarySearchTest {
|
|||
int result = Arrays.binarySearch(nums, target);
|
||||
if (result < 0) {
|
||||
int insertIndex = Math.abs(result + 1);
|
||||
System.out.printf("nums: " + Arrays.toString(nums) + "\n");
|
||||
System.out.printf("insertIndex: " + insertIndex + "\n");
|
||||
System.out.println("nums: " + Arrays.toString(nums));
|
||||
System.out.println("insertIndex: " + insertIndex);
|
||||
int[] newNums = new int[nums.length + 1];
|
||||
//第一次拷贝
|
||||
System.arraycopy(nums, 0, newNums, 0, nums.length);
|
||||
System.out.printf("newNums: " + Arrays.toString(newNums) + "\n");
|
||||
System.out.println("newNums: " + Arrays.toString(newNums));
|
||||
newNums[insertIndex] = target;
|
||||
// 第二次拷贝
|
||||
System.arraycopy(nums, insertIndex, newNums, insertIndex + 1, nums.length - insertIndex);
|
||||
System.out.printf("newNums: " + Arrays.toString(newNums) + "\n");
|
||||
System.out.println("newNums: " + Arrays.toString(newNums));
|
||||
}
|
||||
System.out.printf("result: " + result);
|
||||
System.out.println("result: " + result);
|
||||
assertEquals(-3, result, "The target should not be found");
|
||||
}
|
||||
|
||||
|
|
@ -126,4 +126,14 @@ public class BinarySearchTest {
|
|||
assertEquals(2, BinarySearch.binarySearchRightMost(nums, 3));
|
||||
assertEquals(7, BinarySearch.binarySearchRightMost(nums, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchRange() {
|
||||
int[] nums = {5, 7, 7, 8, 8, 10};
|
||||
int[] nums1 = {1};
|
||||
assertArrayEquals(new int[]{1,2}, BinarySearch.searchRange(nums, 7), "The result should be [1, 2]" );
|
||||
assertArrayEquals(new int[]{3,4}, BinarySearch.searchRange(nums, 8), "The result should be [3, 4]" );
|
||||
assertArrayEquals(new int[]{-1, -1}, BinarySearch.searchRange(nums, 6), "The result should be [-1, -1]");
|
||||
assertArrayEquals(new int[]{0, 0}, BinarySearch.searchRange(nums1, 1), "The result should be [0, 0]");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
package com.nbee.solution.practice;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DynamicArrayTest {
|
||||
|
||||
private DynamicArray dynamicArray;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
dynamicArray = new DynamicArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddListInitial() {
|
||||
dynamicArray.addList(1);
|
||||
dynamicArray.addList(3);
|
||||
dynamicArray.addList(5);
|
||||
for (int i = 0; i < 3; i++){
|
||||
System.out.println(dynamicArray.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testAddListMultiple() {
|
||||
dynamicArray.addList(2);
|
||||
dynamicArray.addList(3);
|
||||
dynamicArray.addList(4);
|
||||
assertEquals(3, dynamicArray.getSize(), "Size should be 3 after adding three elements.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddListCapacityExceeded() {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
dynamicArray.addList(i);
|
||||
}
|
||||
assertEquals(8, dynamicArray.getSize(), "Size should be 8 after adding elements up to capacity.");
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue