Compare commits
10 Commits
5d29f810a2
...
45b40688c7
| Author | SHA1 | Date |
|---|---|---|
|
|
45b40688c7 | |
|
|
438abeca95 | |
|
|
6fb29ad89e | |
|
|
1d77960011 | |
|
|
732d006f3b | |
|
|
e27cb1fb87 | |
|
|
d589c45e45 | |
|
|
97eb4196e9 | |
|
|
23dc31f9ae | |
|
|
a4fd3e2a15 |
|
|
@ -0,0 +1,22 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.nbee</groupId>
|
||||||
|
<artifactId>solution</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.8.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
class BinarySearch {
|
||||||
|
public static int binarySearchBasic(int[] nums, int target){
|
||||||
|
int i = 0 ,j = nums.length - 1;//设置指针和初始值
|
||||||
|
while (i <= j){
|
||||||
|
int mid = ((i + j) / 2);
|
||||||
|
if (target == nums[mid]){
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
else if (target < nums[mid]){
|
||||||
|
j = mid - 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
j = mid + 1;
|
||||||
|
}
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package solution;
|
package com.nbee.solution.solution;
|
||||||
|
|
||||||
public class ListNode {
|
public class ListNode {
|
||||||
int val;
|
int val;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package solution;
|
package com.nbee.solution.solution;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
package solution;
|
package com.nbee.solution.solution;
|
||||||
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class test {
|
public class test {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ListNode head = new ListNode(1);
|
ListNode head = new ListNode(1);
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class BinarySearchTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_Found() {
|
||||||
|
int[] nums = {1, 3, 5, 7, 9};
|
||||||
|
int target = 5;
|
||||||
|
int expected = 2;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
assertEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_NotFound() {
|
||||||
|
int[] nums = {1, 3, 5, 7, 9};
|
||||||
|
int target = 4;
|
||||||
|
int expected = -1;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
assertEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_EmptyArray() {
|
||||||
|
int[] nums = {};
|
||||||
|
int target = 0;
|
||||||
|
int expected = -1;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
assertEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_SingleElementFound() {
|
||||||
|
int[] nums = {2};
|
||||||
|
int target = 2;
|
||||||
|
int expected = 0;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
assertEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_SingleElementNotFound() {
|
||||||
|
int[] nums = {2};
|
||||||
|
int target = 3;
|
||||||
|
int expected = -1;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
assertEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_DecreasingArray() {
|
||||||
|
int[] nums = {9, 7, 5, 3, 1};
|
||||||
|
int target = 3;
|
||||||
|
int expected = 2;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
assertEquals(expected, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue