Skip to content

Commit e815258

Browse files
committed
add BinarySearchSolution and BinarySearchSolutionTest
1 parent af905f0 commit e815258

File tree

4 files changed

+136
-13
lines changed

4 files changed

+136
-13
lines changed

pom.xml

+12-6
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
<lombok.version>1.16.8</lombok.version>
3939

4040
<!-- Test -->
41-
<junit.version>5.7.2</junit.version>
42-
<testng.version>7.3.0</testng.version>
41+
<junit-jupiter.version>5.7.2</junit-jupiter.version>
42+
<junit.version>4.13.2</junit.version>
43+
<testng.version>7.4.0</testng.version>
4344
<assertj.version>3.20.2</assertj.version>
4445
<mockito.version>3.11.2</mockito.version>
4546

@@ -198,6 +199,11 @@
198199
<dependency>
199200
<groupId>org.junit.jupiter</groupId>
200201
<artifactId>junit-jupiter</artifactId>
202+
<version>${junit-jupiter.version}</version>
203+
</dependency>
204+
<dependency>
205+
<groupId>junit</groupId>
206+
<artifactId>junit</artifactId>
201207
<version>${junit.version}</version>
202208
</dependency>
203209
<!-- TestNG Documentation - http://testng.org/doc/documentation-main.html -->
@@ -357,22 +363,22 @@
357363
<dependency>
358364
<groupId>org.junit.jupiter</groupId>
359365
<artifactId>junit-jupiter</artifactId>
360-
<!-- <scope>test</scope>-->
366+
</dependency>
367+
<dependency>
368+
<groupId>junit</groupId>
369+
<artifactId>junit</artifactId>
361370
</dependency>
362371
<dependency>
363372
<groupId>org.testng</groupId>
364373
<artifactId>testng</artifactId>
365-
<!-- <scope>test</scope>-->
366374
</dependency>
367375
<dependency>
368376
<groupId>org.assertj</groupId>
369377
<artifactId>assertj-core</artifactId>
370-
<!-- <scope>test</scope>-->
371378
</dependency>
372379
<dependency>
373380
<groupId>org.mockito</groupId>
374381
<artifactId>mockito-core</artifactId>
375-
<!-- <scope>test</scope>-->
376382
</dependency>
377383

378384
<!-- Logging -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package leet.code.search.binary;
2+
3+
/**
4+
* <a href="https://leetcode-cn.com/problems/binary-search/">
5+
* 704. 二分查找</a>
6+
*
7+
* @author guangyi
8+
* @since 2021-12-26
9+
*/
10+
public class BinarySearchSolution {
11+
12+
/**
13+
* 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,
14+
* 写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
15+
*
16+
* @param nums 有序的的整型数组,升序
17+
* @param target 目标值
18+
* @return 目标值所在的数组下标,未找到返回-1
19+
*/
20+
public static int search(int[] nums, int target) {
21+
if (nums == null || nums.length <= 0) {
22+
return -1;
23+
}
24+
return binarySearch(nums, 0, nums.length - 1, target);
25+
}
26+
27+
/**
28+
* 递归地二分搜索。
29+
*
30+
* @param nums 有序的的整型数组,升序
31+
* @param startIndex 起始下标,包含
32+
* @param endIndex 终止下标,包含
33+
* @param target 目标值
34+
* @return 目标值所在的数组下标,未找到返回-1
35+
*/
36+
public static int binarySearch(int[] nums, int startIndex, int endIndex, int target) {
37+
// 递归搜索的终止条件
38+
if (startIndex > endIndex) {
39+
return -1;
40+
} else if (startIndex == endIndex) {
41+
if (nums[startIndex] == target) {
42+
// 发现目标值
43+
return startIndex;
44+
}
45+
return -1;
46+
}
47+
// 中区下标
48+
int midIndex = (startIndex + endIndex) / 2;
49+
int num = nums[midIndex];
50+
if (num == target) {
51+
// 发现目标值
52+
return midIndex;
53+
} else if (num < target) {
54+
// 递归地二分搜索下半区
55+
return binarySearch(nums, midIndex + 1, endIndex, target);
56+
} else {
57+
// 递归地二分搜索上半区
58+
return binarySearch(nums, startIndex, midIndex, target);
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leet.code.search.binary;
2+
3+
import org.testng.annotations.DataProvider;
4+
import org.testng.annotations.Test;
5+
6+
import static org.assertj.core.api.Assertions.*;
7+
8+
/**
9+
* Test of {@link BinarySearchSolution}.
10+
*
11+
* @author guangyi
12+
* @since 2021-12-26
13+
*/
14+
public class BinarySearchSolutionTest {
15+
16+
@Test(dataProvider = "searchTestData")
17+
public void search(int[] numbers, int target, int expectedIndex) {
18+
int index = BinarySearchSolution.search(numbers, target);
19+
assertThat(index).isEqualTo(expectedIndex);
20+
}
21+
22+
@DataProvider(name = "searchTestData")
23+
private static Object[][] searchTestData() {
24+
return new Object[][]{
25+
// 偶数个
26+
{new int[]{-1, 0, 3, 5, 9, 12}, 9, 4},
27+
{new int[]{-1, 0, 3, 5, 9, 12}, 12, 5},
28+
{new int[]{-1, 0, 3, 5, 9, 12}, 5, 3},
29+
{new int[]{-1, 0, 3, 5, 9, 12}, 3, 2},
30+
{new int[]{-1, 0, 3, 5, 9, 12}, 0, 1},
31+
{new int[]{-1, 0, 3, 5, 9, 12}, -1, 0},
32+
// 未找到
33+
{new int[]{-1, 0, 3, 5, 9, 12}, 13, -1},
34+
{new int[]{-1, 0, 3, 5, 9, 12}, 10, -1},
35+
{new int[]{-1, 0, 3, 5, 9, 12}, 7, -1},
36+
{new int[]{-1, 0, 3, 5, 9, 12}, 2, -1},
37+
{new int[]{-1, 0, 3, 5, 9, 12}, -3, -1},
38+
// 奇数个
39+
{new int[]{3}, 3, 0},
40+
{new int[]{3}, 5, -1},
41+
{new int[]{3}, 1, -1},
42+
{new int[]{1, 5}, 1, 0},
43+
{new int[]{1, 5}, 5, 1},
44+
{new int[]{1, 5}, 0, -1},
45+
{new int[]{1, 5}, 3, -1},
46+
{new int[]{1, 5}, 7, -1},
47+
{new int[]{1, 3, 5}, 1, 0},
48+
{new int[]{1, 3, 5}, 3, 1},
49+
{new int[]{1, 3, 5}, 5, 2},
50+
{new int[]{1, 3, 5}, 0, -1},
51+
{new int[]{1, 3, 5}, 2, -1},
52+
{new int[]{1, 3, 5}, 4, -1},
53+
{new int[]{1, 3, 5}, 6, -1},
54+
};
55+
}
56+
}

testng.xml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
2+
<!DOCTYPE suite SYSTEM "https://beust.com/testng/testng-1.0.dtd" >
33

44
<suite name="api-tests" verbose="1">
55
<test name="testng">
6-
<!--<packages>-->
7-
<!--<package name="sun.util"/>-->
8-
<!--</packages>-->
9-
<classes>
10-
<class name="com.google.common.base.OptionalTest"/>
11-
</classes>
6+
<!-- <packages>-->
7+
<!-- <package name="sun.util"/>-->
8+
<!-- </packages>-->
9+
<!-- <classes>-->
10+
<!-- <class name="com.google.common.base.OptionalTest"/>-->
11+
<!-- </classes>-->
1212
</test>
1313
</suite>

0 commit comments

Comments
 (0)