Add the section of binary search insertion. (#671)

Refactor the section of binary search edge.
Finetune the figures of binary search.
This commit is contained in:
Yudong Jin
2023-08-04 05:16:56 +08:00
committed by GitHub
parent 3d81b2d954
commit 71074d88f6
52 changed files with 546 additions and 621 deletions
@@ -1,59 +0,0 @@
/**
* File: binary_search_edge.c
* Created Time: 2023-05-31
* Author: Gonglja (glj0@outlook.com)
*/
#include "../utils/common.h"
/* 二分查找最左一个元素 */
int binarySearchLeftEdge(int *nums, int size, int target) {
int i = 0, j = size - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target)
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
}
if (i == size || nums[i] != target)
return -1; // 未找到目标元素,返回 -1
return i;
}
/* 二分查找最右一个元素 */
int binarySearchRightEdge(int *nums, int size, int target) {
int i = 0, j = size - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target)
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
}
if (j < 0 || nums[j] != target)
return -1; // 未找到目标元素,返回 -1
return j;
}
/* Driver Code */
int main() {
int target = 6;
int nums[] = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
int size = sizeof(nums) / sizeof(nums[0]);
// 二分查找最左一个元素
int indexLeft = binarySearchLeftEdge(nums, size, target);
printf("数组中最左一个元素 6 的索引 = %d\n", indexLeft);
// 二分查找最右一个元素
int indexRight = binarySearchRightEdge(nums, size, target);
printf("数组中最右一个元素 6 的索引 = %d\n", indexRight);
return 0;
}
@@ -1,3 +1,4 @@
add_executable(binary_search binary_search.cpp)
add_executable(binary_search_insertion binary_search_insertion.cpp)
add_executable(binary_search_edge binary_search_edge.cpp)
add_executable(two_sum two_sum.cpp)
@@ -1,57 +1,66 @@
/**
* File: binary_search_edge.cpp
* Created Time: 2023-05-21
* Created Time: 2023-08-04
* Author: Krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* 二分查找最左一个元素 */
int binarySearchLeftEdge(vector<int> &nums, int target) {
/* 二分查找插入点(存在重复元素 */
int binarySearchInsertion(const vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target)
if (nums[m] < target) {
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
} else {
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
}
}
if (i == nums.size() || nums[i] != target)
return -1; // 未找到目标元素,返回 -1
// 返回插入点 i
return i;
}
/* 二分查找最一个元素 */
int binarySearchRightEdge(vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target)
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
/* 二分查找最一个 target */
int binarySearchLeftEdge(vector<int> &nums, int target) {
// 等价于查找 target 的插入点
int i = binarySearchInsertion(nums, target);
// 未找到 target ,返回 -1
if (i == nums.size() || nums[i] != target) {
return -1;
}
if (j < 0 || nums[j] != target)
return -1; // 未找到目标元素,返回 -1
// 找到 target ,返回索引 i
return i;
}
/* 二分查找最右一个 target */
int binarySearchRightEdge(vector<int> &nums, int target) {
// 转化为查找最左一个 target + 1
int i = binarySearchInsertion(nums, target + 1);
// j 指向最右一个 target ,i 指向首个大于 target 的元素
int j = i - 1;
// 未找到 target ,返回 -1
if (j == -1 || nums[j] != target) {
return -1;
}
// 找到 target ,返回索引 j
return j;
}
/* Driver Code */
int main() {
int target = 6;
// 包含重复元素的数组
vector<int> nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
cout << "\n数组 nums = ";
printVector(nums);
// 二分查找最左一个元素
int indexLeft = binarySearchLeftEdge(nums, target);
cout << "数组中最左一个元素 6 的索引 = " << indexLeft << endl;
// 二分查找最右一个元素
int indexRight = binarySearchRightEdge(nums, target);
cout << "数组中最右一个元素 6 的索引 = " << indexRight << endl;
// 二分查找左边界和右边界
for (int target : {6, 7}) {
int index = binarySearchLeftEdge(nums, target);
cout << "最左一个元素 " << target << " 的索引为 " << index << endl;
index = binarySearchRightEdge(nums, target);
cout << "最右一个元素 " << target << " 的索引为 " << index << endl;
}
return 0;
}
@@ -0,0 +1,66 @@
/**
* File: binary_search_edge.cpp
* Created Time: 2023-08-04
* Author: Krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* 二分查找插入点(无重复元素) */
int binarySearchInsertionSimple(vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target) {
i = m + 1; // target 在区间 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
return m; // 找到 target ,返回插入点 m
}
}
// 未找到 target ,返回插入点 i
return i;
}
/* 二分查找插入点(存在重复元素) */
int binarySearchInsertion(vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target) {
i = m + 1; // target 在区间 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
}
}
// 返回插入点 i
return i;
}
/* Driver Code */
int main() {
// 无重复元素的数组
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
cout << "\n数组 nums = ";
printVector(nums);
// 二分查找插入点
for (int target : {6, 9}) {
int index = binarySearchInsertionSimple(nums, target);
cout << "元素 " << target << " 的插入点的索引为 " << index << endl;
}
// 包含重复元素的数组
nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
cout << "\n数组 nums = ";
printVector(nums);
// 二分查找插入点
for (int target : {2, 6, 20}) {
int index = binarySearchInsertion(nums, target);
cout << "元素 " << target << " 的插入点的索引为 " << index << endl;
}
return 0;
}
@@ -1,57 +0,0 @@
/**
* File: binary_search_edge.cs
* Created Time: 2023-06-01
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_searching;
public class binary_search_edge {
/* 二分查找最左一个元素 */
public static int binarySearchLeftEdge(int[] nums, int target) {
int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target)
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
}
if (i == nums.Length || nums[i] != target)
return -1; // 未找到目标元素,返回 -1
return i;
}
/* 二分查找最右一个元素 */
public static int binarySearchRightEdge(int[] nums, int target) {
int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target)
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
}
if (j < 0 || nums[j] != target)
return -1; // 未找到目标元素,返回 -1
return j;
}
[Test]
public void Test() {
int target = 6;
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
// 二分查找最左一个元素
int indexLeft = binarySearchLeftEdge(nums, target);
Console.WriteLine("数组中最左一个元素 6 的索引 = " + indexLeft);
// 二分查找最右一个元素
int indexRight = binarySearchRightEdge(nums, target);
Console.WriteLine("数组中最右一个元素 6 的索引 = " + indexRight);
}
}
@@ -1,50 +0,0 @@
/**
* File: binary_search_edge.dart
* Created Time: 2023-06-01
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* 二分查找最左一个元素 */
int binarySearchLeftEdge(List<int> nums, int target) {
int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) ~/ 2; // 计算中间索引 m
if (nums[m] < target)
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
}
if (i == nums.length || nums[i] != target) return -1; // 未找到目标元素,返回 -1
return i;
}
/* 二分查找最右一个元素 */
int binarySearchRightEdge(List<int> nums, int target) {
int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) ~/ 2; // 计算中间索引 m
if (nums[m] < target)
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
}
if (j < 0 || nums[j] != target) return -1; // 未找到目标元素,返回 -1
return j;
}
/* Driver Code */
void main() {
int target = 6;
List<int> nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
// 二分查找最左一个元素
int indexLeft = binarySearchLeftEdge(nums, target);
print("数组中最左一个元素 6 的索引 = $indexLeft");
// 二分查找最右一个元素
int indexRight = binarySearchRightEdge(nums, target);
print("数组中最右一个元素 6 的索引 = $indexRight");
}
@@ -1,55 +0,0 @@
// File: binary_search_edge.go
// Created Time: 2023-05-29
// Author: Reanon (793584285@qq.com)
package chapter_searching
/* 二分查找最左一个元素 */
func binarySearchLeftEdge(nums []int, target int) int {
// 初始化双闭区间 [0, n-1]
i, j := 0, len(nums)-1
for i <= j {
// 计算中点索引 m
m := i + (j-i)/2
if nums[m] < target {
// target 在区间 [m+1, j] 中
i = m + 1
} else if nums[m] > target {
// target 在区间 [i, m-1] 中
j = m - 1
} else {
// 首个小于 target 的元素在区间 [i, m-1] 中
j = m - 1
}
}
if i == len(nums) || nums[i] != target {
// 未找到目标元素,返回 -1
return -1
}
return i
}
/* 二分查找最右一个元素 */
func binarySearchRightEdge(nums []int, target int) int {
// 初始化双闭区间 [0, n-1]
i, j := 0, len(nums)-1
for i <= j {
// 计算中点索引 m
m := i + (j-i)/2
if nums[m] < target {
// target 在区间 [m+1, j] 中
i = m + 1
} else if nums[m] > target {
// target 在区间 [i, m-1] 中
j = m - 1
} else {
// 首个大于 target 的元素在区间 [m+1, j] 中
i = m + 1
}
}
if j < 0 || nums[j] != target {
// 未找到目标元素,返回 -1
return -1
}
return j
}
@@ -22,15 +22,3 @@ func TestBinarySearch(t *testing.T) {
t.Errorf("目标元素 6 的索引 = %d, 应该为 %d", actual, expected)
}
}
func TestBinarySearchEdge(t *testing.T) {
target := 6
nums := []int{1, 3, 6, 6, 6, 6, 6, 10, 12, 15}
// 二分查找最左一个元素
indexLeft := binarySearchLeftEdge(nums, target)
fmt.Println("数组中最左一个元素 6 的索引 = ", indexLeft)
// 二分查找最右一个元素
indexRight := binarySearchRightEdge(nums, target)
fmt.Println("数组中最右一个元素 6 的索引 = ", indexRight)
}
@@ -1,56 +1,49 @@
/**
* File: binary_search_edge.java
* Created Time: 2023-05-21
* Created Time: 2023-08-04
* Author: Krahets (krahets@163.com)
*/
package chapter_searching;
public class binary_search_edge {
/* 二分查找最左一个元素 */
/* 二分查找最左一个 target */
static int binarySearchLeftEdge(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target)
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
// 等价于查找 target 的插入点
int i = binary_search_insertion.binarySearchInsertion(nums, target);
// 未找到 target ,返回 -1
if (i == nums.length || nums[i] != target) {
return -1;
}
if (i == nums.length || nums[i] != target)
return -1; // 未找到目标元素,返回 -1
// 找到 target ,返回索引 i
return i;
}
/* 二分查找最右一个元素 */
/* 二分查找最右一个 target */
static int binarySearchRightEdge(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target)
i = m + 1; // target 在区间 [m+1, j] 中
else if (nums[m] > target)
j = m - 1; // target 在区间 [i, m-1] 中
else
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
// 转化为查找最左一个 target + 1
int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
// j 指向最右一个 target ,i 指向首个大于 target 的元素
int j = i - 1;
// 未找到 target ,返回 -1
if (j == -1 || nums[j] != target) {
return -1;
}
if (j < 0 || nums[j] != target)
return -1; // 未找到目标元素,返回 -1
// 找到 target ,返回索引 j
return j;
}
public static void main(String[] args) {
int target = 6;
// 包含重复元素的数组
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
System.out.println("\n数组 nums = " + java.util.Arrays.toString(nums));
// 二分查找最左一个元素
int indexLeft = binarySearchLeftEdge(nums, target);
System.out.println("数组中最左一个元素 6 的索引 = " + indexLeft);
// 二分查找最右一个元素
int indexRight = binarySearchRightEdge(nums, target);
System.out.println("数组中最右一个元素 6 的索引 = " + indexRight);
// 二分查找左边界和右边界
for (int target : new int[] { 6, 7 }) {
int index = binarySearchLeftEdge(nums, target);
System.out.println("最左一个元素 " + target + " 的索引为 " + index);
index = binarySearchRightEdge(nums, target);
System.out.println("最右一个元素 " + target + " 的索引为 " + index);
}
}
}
@@ -0,0 +1,63 @@
/**
* File: binary_search_edge.java
* Created Time: 2023-08-04
* Author: Krahets (krahets@163.com)
*/
package chapter_searching;
class binary_search_insertion {
/* 二分查找插入点(无重复元素) */
static int binarySearchInsertionSimple(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target) {
i = m + 1; // target 在区间 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
return m; // 找到 target ,返回插入点 m
}
}
// 未找到 target ,返回插入点 i
return i;
}
/* 二分查找插入点(存在重复元素) */
static int binarySearchInsertion(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target) {
i = m + 1; // target 在区间 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
}
}
// 返回插入点 i
return i;
}
public static void main(String[] args) {
// 无重复元素的数组
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
System.out.println("\n数组 nums = " + java.util.Arrays.toString(nums));
// 二分查找插入点
for (int target : new int[] { 6, 9 }) {
int index = binarySearchInsertionSimple(nums, target);
System.out.println("元素 " + target + " 的插入点的索引为 " + index);
}
// 包含重复元素的数组
nums = new int[] { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
System.out.println("\n数组 nums = " + java.util.Arrays.toString(nums));
// 二分查找插入点
for (int target : new int[] { 2, 6, 20 }) {
int index = binarySearchInsertion(nums, target);
System.out.println("元素 " + target + " 的插入点的索引为 " + index);
}
}
}
@@ -1,57 +0,0 @@
/**
* File: binary_search_edge.js
* Created Time: 2023-06-04
* Author: Justin (xiefahit@gmail.com)
*/
/* 二分查找最左一个元素 */
function binarySearchLeftEdge(nums, target) {
let i = 0,
j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
let m = Math.floor((i + j) / 2); // 计算中点索引 m
if (nums[m] < target) {
i = m + 1; // target 在区间 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
}
}
if (i === nums.length || nums[i] != target) {
return -1; // 未找到目标元素,返回 -1
}
return i;
}
/* 二分查找最右一个元素 */
function binarySearchRightEdge(nums, target) {
let i = 0,
j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
let m = Math.floor((i + j) / 2); // 计算中点索引 m
if (nums[m] < target) {
i = m + 1; // target 在区间 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
}
}
if (j < 0 || nums[j] != target) {
return -1; // 未找到目标元素,返回 -1
}
return j;
}
/* Driver Code */
let target = 6;
const nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
// 二分查找最左一个元素
let index_left = binarySearchLeftEdge(nums, target);
console.log('数组中最左一个元素 6 的索引 = ', index_left);
// 二分查找最右一个元素
let index_right = binarySearchRightEdge(nums, target);
console.log('数组中最右一个元素 6 的索引 = ', index_right);
@@ -1,51 +1,48 @@
"""
File: binary_search_edge.py
Created Time: 2023-05-18
Created Time: 2023-08-04
Author: Krahets (krahets@163.com)
"""
import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from binary_search_insertion import binary_search_insertion
def binary_search_left_edge(nums: list[int], target: int) -> int:
"""二分查找最左一个元素"""
i, j = 0, len(nums) - 1 # 初始化双闭区间 [0, n-1]
while i <= j:
m = (i + j) // 2 # 计算中点索引 m
if nums[m] < target:
i = m + 1 # target 在区间 [m+1, j] 中
elif nums[m] > target:
j = m - 1 # target 在区间 [i, m-1] 中
else:
j = m - 1 # 首个小于 target 的元素在区间 [i, m-1] 中
"""二分查找最左一个 target"""
# 等价于查找 target 的插入点
i = binary_search_insertion(nums, target)
# 未找到 target ,返回 -1
if i == len(nums) or nums[i] != target:
return -1 # 未找到目标元素,返回 -1
return -1
# 找到 target ,返回索引 i
return i
def binary_search_right_edge(nums: list[int], target: int) -> int:
"""二分查找最右一个元素"""
i, j = 0, len(nums) - 1 # 初始化双闭区间 [0, n-1]
while i <= j:
m = (i + j) // 2 # 计算中点索引 m
if nums[m] < target:
i = m + 1 # target 在区间 [m+1, j] 中
elif nums[m] > target:
j = m - 1 # target 在区间 [i, m-1] 中
else:
i = m + 1 # 首个大于 target 的元素在区间 [m+1, j] 中
if j < 0 or nums[j] != target:
return -1 # 未找到目标元素,返回 -1
"""二分查找最右一个 target"""
# 转化为查找最左一个 target + 1
i = binary_search_insertion(nums, target + 1)
# j 指向最右一个 target ,i 指向首个大于 target 的元素
j = i - 1
# 未找到 target ,返回 -1
if j == -1 or nums[j] != target:
return -1
# 找到 target ,返回索引 j
return j
"""Driver Code"""
if __name__ == "__main__":
target = 6
# 包含重复元素的数组
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
print(f"\n数组 nums = {nums}")
# 二分查找最左一个元素
index_left = binary_search_left_edge(nums, target)
print("数组中最左一个元素 6 的索引 = ", index_left)
# 二分查找最右一个元素
index_right = binary_search_right_edge(nums, target)
print("数组中最右一个元素 6 的索引 = ", index_right)
# 二分查找左边界和右边界
for target in [6, 7]:
index = binary_search_left_edge(nums, target)
print(f"最左一个元素 {target} 的索引为 {index}")
index = binary_search_right_edge(nums, target)
print(f"最右一个元素 {target} 的索引为 {index}")
@@ -0,0 +1,54 @@
"""
File: binary_search_insertion.py
Created Time: 2023-08-04
Author: Krahets (krahets@163.com)
"""
def binary_search_insertion_simple(nums: list[int], target: int) -> int:
"""二分查找插入点(无重复元素)"""
i, j = 0, len(nums) - 1 # 初始化双闭区间 [0, n-1]
while i <= j:
m = (i + j) // 2 # 计算中点索引 m
if nums[m] < target:
i = m + 1 # target 在区间 [m+1, j] 中
elif nums[m] > target:
j = m - 1 # target 在区间 [i, m-1] 中
else:
return m # 找到 target ,返回插入点 m
# 未找到 target ,返回插入点 i
return i
def binary_search_insertion(nums: list[int], target: int) -> int:
"""二分查找插入点(存在重复元素)"""
i, j = 0, len(nums) - 1 # 初始化双闭区间 [0, n-1]
while i <= j:
m = (i + j) // 2 # 计算中点索引 m
if nums[m] < target:
i = m + 1 # target 在区间 [m+1, j] 中
elif nums[m] > target:
j = m - 1 # target 在区间 [i, m-1] 中
else:
j = m - 1 # 首个小于 target 的元素在区间 [i, m-1] 中
# 返回插入点 i
return i
"""Driver Code"""
if __name__ == "__main__":
# 无重复元素的数组
nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
print(f"\n数组 nums = {nums}")
# 二分查找插入点
for target in [6, 9]:
index = binary_search_insertion_simple(nums, target)
print(f"元素 {target} 的插入点的索引为 {index}")
# 包含重复元素的数组
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
print(f"\n数组 nums = {nums}")
# 二分查找插入点
for target in [2, 6, 20]:
index = binary_search_insertion(nums, target)
print(f"元素 {target} 的插入点的索引为 {index}")
@@ -1,59 +0,0 @@
/*
* File: binary_search_edge.rs
* Created Time: 2023-05-31
* Author: WSL0809 (wslzzy@outlook.com)
*/
/* 二分查找最左一个元素 */
fn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {
let mut i = 0;
let mut j = nums.len() as i32 - 1; // 初始化双闭区间 [0, n-1]
while i <= j {
let m = i + (j - i) / 2; // 计算中点索引 m
if nums[m as usize] < target {
i = m + 1; // target 在区间 [m+1, j] 中
} else if nums[m as usize] > target {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
}
}
if i == nums.len() as i32 || nums[i as usize] != target {
return -1; // 未找到目标元素,返回 -1
}
i
}
/* 二分查找最右一个元素 */
fn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {
let mut i = 0;
let mut j = nums.len() as i32 - 1; // 初始化双闭区间 [0, n-1]
while i <= j {
let m = i + (j - i) / 2; // 计算中点索引 m
if nums[m as usize] < target {
i = m + 1; // target 在区间 [m+1, j] 中
} else if nums[m as usize] > target {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
}
}
if j < 0 || nums[j as usize] != target {
return -1; // 未找到目标元素,返回 -1
}
j
}
/* Driver Code */
pub fn main() {
let target = 6;
let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
// 二分查找最左一个元素
let index_left = binary_search_left_edge(&nums, target);
println!("数组中最左一个元素 6 的索引 = {}", index_left);
// 二分查找最右一个元素
let index_right = binary_search_right_edge(&nums, target);
println!("数组中最右一个元素 6 的索引 = {}", index_right);
}
@@ -1,64 +0,0 @@
/**
* File: binary_search_edge.swift
* Created Time: 2023-05-28
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func binarySearchLeftEdge(nums: [Int], target: Int) -> Int {
// [0, n-1]
var i = 0
var j = nums.count - 1
while i <= j {
let m = i + (j - 1) / 2 // m
if nums[m] < target {
i = m + 1 // target [m+1, j]
} else if nums[m] > target {
j = m - 1 // target [i, m-1]
} else {
j = m - 1 // target [i, m-1]
}
}
if i == nums.count || nums[i] != target {
return -1 // -1
}
return i
}
/* */
func binarySearchRightEdge(nums: [Int], target: Int) -> Int {
// [0, n-1]
var i = 0
var j = nums.count - 1
while i <= j {
let m = i + (j - i) / 2 // m
if nums[m] < target {
i = m + 1 // target [m+1, j]
} else if nums[m] > target {
j = m - 1 // target [i, m-1]
} else {
i = m + 1 // target [m+1, j]
}
}
if j < 0 || nums[j] != target {
return -1 // -1
}
return j
}
@main
enum BinarySearchEdge {
/* Driver Code */
static func main() {
let target = 6
let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
//
let indexLeft = binarySearchLeftEdge(nums: nums, target: target)
print("数组中最左一个元素 6 的索引 = \(indexLeft)")
//
let indexRight = binarySearchRightEdge(nums: nums, target: target)
print("数组中最右一个元素 6 的索引 = \(indexRight)")
}
}
@@ -1,55 +0,0 @@
/**
* File: binary_search_edge.ts
* Created Time: 2023-06-04
* Author: Justin (xiefahit@gmail.com)
*/
/* 二分查找最左一个元素 */
function binarySearchLeftEdge(nums: number[], target: number): number {
let i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
let m = Math.floor((i + j) / 2); // 计算中点索引 m
if (nums[m] < target) {
i = m + 1; // target 在区间 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
}
}
if (i === nums.length || nums[i] != target) {
return -1; // 未找到目标元素,返回 -1
}
return i;
}
/* 二分查找最右一个元素 */
function binarySearchRightEdge(nums: number[], target: number): number {
let i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
let m = Math.floor((i + j) / 2); // 计算中点索引 m
if (nums[m] < target) {
i = m + 1; // target 在区间 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在区间 [i, m-1] 中
} else {
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
}
}
if (j < 0 || nums[j] != target) {
return -1; // 未找到目标元素,返回 -1
}
return j;
}
/* Driver Code */
let target: number = 6;
const nums: number[] = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
// 二分查找最左一个元素
let index_left: number = binarySearchLeftEdge(nums, target);
console.log("数组中最左一个元素 6 的索引 = ", index_left);
// 二分查找最右一个元素
let index_right: number = binarySearchRightEdge(nums, target);
console.log("数组中最右一个元素 6 的索引 = ", index_right);