mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:46:29 +08:00
Fix the test case of binary search.
This commit is contained in:
parent
d3cc149c5a
commit
d95c628eef
13 changed files with 36 additions and 12 deletions
|
@ -45,7 +45,7 @@ int binarySearchLCRO(int *nums, int len, int target) {
|
|||
/* Driver Code */
|
||||
int main() {
|
||||
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);
|
||||
|
|
|
@ -45,7 +45,7 @@ int binarySearchLCRO(vector<int> &nums, int target) {
|
|||
/* Driver Code */
|
||||
int main() {
|
||||
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);
|
||||
|
|
|
@ -46,7 +46,7 @@ public class binary_search {
|
|||
[Test]
|
||||
public void Test() {
|
||||
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);
|
||||
|
|
|
@ -51,7 +51,7 @@ int binarySearchLCRO(List<int> nums, int target) {
|
|||
/* Driver Code*/
|
||||
void main() {
|
||||
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);
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
func TestBinarySearch(t *testing.T) {
|
||||
var (
|
||||
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
|
||||
)
|
||||
// 在数组中执行二分查找
|
||||
|
|
|
@ -45,7 +45,7 @@ public class binary_search {
|
|||
|
||||
public static void main(String[] args) {
|
||||
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);
|
||||
|
|
|
@ -49,7 +49,7 @@ function binarySearchLCRO(nums, target) {
|
|||
|
||||
/* Driver Code */
|
||||
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);
|
||||
|
|
|
@ -41,7 +41,7 @@ def binary_search_lcro(nums: list[int], target: int) -> int:
|
|||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
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)
|
||||
|
|
24
codes/python/chapter_searching/binary_search_rotation.py
Normal file
24
codes/python/chapter_searching/binary_search_rotation.py
Normal 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)
|
|
@ -47,7 +47,7 @@ fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
|
|||
/* Driver Code */
|
||||
pub fn main() {
|
||||
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);
|
||||
|
|
|
@ -49,7 +49,7 @@ enum BinarySearch {
|
|||
/* Driver Code */
|
||||
static func main() {
|
||||
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)
|
||||
|
|
|
@ -52,7 +52,7 @@ function binarySearchLCRO(nums: number[], target: number): number {
|
|||
|
||||
/* Driver Code */
|
||||
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);
|
||||
|
|
|
@ -50,7 +50,7 @@ pub fn main() !void {
|
|||
var target: i32 = 6;
|
||||
var nums = std.ArrayList(i32).init(std.heap.page_allocator);
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue