Fix the test case of binary search.

This commit is contained in:
krahets 2023-05-21 19:29:24 +08:00
parent d3cc149c5a
commit d95c628eef
13 changed files with 36 additions and 12 deletions

View file

@ -45,7 +45,7 @@ int binarySearchLCRO(int *nums, int len, int target) {
/* Driver Code */ /* Driver Code */
int main() { int main() {
int target = 6; int target = 6;
int nums[10] = {1, 3, 6, 8, 12, 15, 23, 67, 70, 92}; int nums[10] = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
int index = binarySearch(nums, 10, target); int index = binarySearch(nums, 10, target);

View file

@ -45,7 +45,7 @@ int binarySearchLCRO(vector<int> &nums, int target) {
/* Driver Code */ /* Driver Code */
int main() { int main() {
int target = 6; int target = 6;
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 67, 70, 92}; vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
int index = binarySearch(nums, target); int index = binarySearch(nums, target);

View file

@ -46,7 +46,7 @@ public class binary_search {
[Test] [Test]
public void Test() { public void Test() {
int target = 6; int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 }; int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
int index = binarySearch(nums, target); int index = binarySearch(nums, target);

View file

@ -51,7 +51,7 @@ int binarySearchLCRO(List<int> nums, int target) {
/* Driver Code*/ /* Driver Code*/
void main() { void main() {
int target = 6; int target = 6;
final nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]; final nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
/* 二分查找 (双闭区间) */ /* 二分查找 (双闭区间) */
int index = binarySearch(nums, target); int index = binarySearch(nums, target);

View file

@ -12,7 +12,7 @@ import (
func TestBinarySearch(t *testing.T) { func TestBinarySearch(t *testing.T) {
var ( var (
target = 6 target = 6
nums = []int{1, 3, 6, 8, 12, 15, 23, 67, 70, 92} nums = []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
expected = 2 expected = 2
) )
// 在数组中执行二分查找 // 在数组中执行二分查找

View file

@ -45,7 +45,7 @@ public class binary_search {
public static void main(String[] args) { public static void main(String[] args) {
int target = 6; int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 }; int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
int index = binarySearch(nums, target); int index = binarySearch(nums, target);

View file

@ -49,7 +49,7 @@ function binarySearchLCRO(nums, target) {
/* Driver Code */ /* Driver Code */
const target = 6; const target = 6;
const nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]; const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
let index = binarySearch(nums, target); let index = binarySearch(nums, target);

View file

@ -41,7 +41,7 @@ def binary_search_lcro(nums: list[int], target: int) -> int:
"""Driver Code""" """Driver Code"""
if __name__ == "__main__": if __name__ == "__main__":
target: int = 6 target: int = 6
nums: list[int] = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92] nums: list[int] = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
# 二分查找(双闭区间) # 二分查找(双闭区间)
index: int = binary_search(nums, target) index: int = binary_search(nums, target)

View file

@ -0,0 +1,24 @@
def binary_search_rotation(nums: list[int]) -> int:
"""二分查找左边界(双闭区间)"""
# 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
i, j = 0, len(nums) - 1
while True:
m = (i + j) // 2 # 计算中点索引 m
if nums[m] < nums[j]:
j = m # 此情况说明 target 在区间 [m+1, j] 中
elif nums[m] > nums[j]:
i = m + 1 # 此情况说明 target 在区间 [i, m-1] 中
else:
return i # 此情况说明 i == j == m ,找到旋转点
"""Driver Code"""
if __name__ == "__main__":
nums: list[int] = [23, 67, 70, 92, 1, 3, 6, 8, 12, 15]
# 二分查找左边界
index_left = binary_search_rotation(nums)
print("数组旋转点的索引 = ", index_left)

View file

@ -47,7 +47,7 @@ fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
/* Driver Code */ /* Driver Code */
pub fn main() { pub fn main() {
let target = 6; let target = 6;
let nums = [ 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 ]; let nums = [ 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 ];
// 二分查找(双闭区间) // 二分查找(双闭区间)
let mut index = binary_search(&nums, target); let mut index = binary_search(&nums, target);

View file

@ -49,7 +49,7 @@ enum BinarySearch {
/* Driver Code */ /* Driver Code */
static func main() { static func main() {
let target = 6 let target = 6
let nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92] let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
/* */ /* */
var index = binarySearch(nums: nums, target: target) var index = binarySearch(nums: nums, target: target)

View file

@ -52,7 +52,7 @@ function binarySearchLCRO(nums: number[], target: number): number {
/* Driver Code */ /* Driver Code */
const target = 6; const target = 6;
const nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]; const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
let index = binarySearch(nums, target); let index = binarySearch(nums, target);

View file

@ -50,7 +50,7 @@ pub fn main() !void {
var target: i32 = 6; var target: i32 = 6;
var nums = std.ArrayList(i32).init(std.heap.page_allocator); var nums = std.ArrayList(i32).init(std.heap.page_allocator);
defer nums.deinit(); defer nums.deinit();
try nums.appendSlice(&[_]i32{ 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 }); try nums.appendSlice(&[_]i32{ 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 });
// //
var index = binarySearch(i32, nums, target); var index = binarySearch(i32, nums, target);