Compare commits

...

10 Commits

Author SHA1 Message Date
wsy182 45b40688c7 Create BinarySearchTest.java 2024-07-09 22:46:10 +08:00
wsy182 438abeca95 Delete test.java 2024-07-09 22:46:07 +08:00
wsy182 6fb29ad89e Delete Solution.java 2024-07-09 22:46:05 +08:00
wsy182 1d77960011 Delete ListNode.java 2024-07-09 22:46:03 +08:00
wsy182 732d006f3b Create test.java 2024-07-09 22:46:00 +08:00
wsy182 e27cb1fb87 Create Solution.java 2024-07-09 22:45:58 +08:00
wsy182 d589c45e45 Create ListNode.java 2024-07-09 22:45:55 +08:00
wsy182 97eb4196e9 Create BinarySearch.java 2024-07-09 22:45:53 +08:00
wsy182 23dc31f9ae Update README.md 2024-07-09 22:45:50 +08:00
wsy182 a4fd3e2a15 Create pom.xml 2024-07-09 22:45:40 +08:00
7 changed files with 110 additions and 7 deletions

View File

@ -1,2 +1,2 @@
# solution.Solution
# main.practice.Solution

22
pom.xml Normal file
View File

@ -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>

View File

@ -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;
}
}

View File

@ -1,4 +1,4 @@
package solution;
package com.nbee.solution.solution;
public class ListNode {
int val;

View File

@ -1,4 +1,4 @@
package solution;
package com.nbee.solution.solution;
import java.util.*;
import java.util.stream.Collectors;

View File

@ -1,9 +1,6 @@
package solution;
package com.nbee.solution.solution;
import java.util.*;
import java.util.stream.Collectors;
public class test {
public static void main(String[] args) {
ListNode head = new ListNode(1);

View File

@ -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);
}
}