diff --git a/codes/c/chapter_searching/binary_search_edge.c b/codes/c/chapter_searching/binary_search_edge.c deleted file mode 100644 index 077317ca..00000000 --- a/codes/c/chapter_searching/binary_search_edge.c +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/codes/cpp/chapter_searching/CMakeLists.txt b/codes/cpp/chapter_searching/CMakeLists.txt index d65ee314..60a223d8 100644 --- a/codes/cpp/chapter_searching/CMakeLists.txt +++ b/codes/cpp/chapter_searching/CMakeLists.txt @@ -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) diff --git a/codes/cpp/chapter_searching/binary_search_edge.cpp b/codes/cpp/chapter_searching/binary_search_edge.cpp index 332fe6c1..494e380d 100644 --- a/codes/cpp/chapter_searching/binary_search_edge.cpp +++ b/codes/cpp/chapter_searching/binary_search_edge.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 &nums, int target) { +/* 二分查找插入点(存在重复元素) */ +int binarySearchInsertion(const vector &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 &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 &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 &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 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; } diff --git a/codes/cpp/chapter_searching/binary_search_insertion.cpp b/codes/cpp/chapter_searching/binary_search_insertion.cpp new file mode 100644 index 00000000..a5e1a178 --- /dev/null +++ b/codes/cpp/chapter_searching/binary_search_insertion.cpp @@ -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 &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 &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 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; +} diff --git a/codes/csharp/chapter_searching/binary_search_edge.cs b/codes/csharp/chapter_searching/binary_search_edge.cs deleted file mode 100644 index 081cc6a1..00000000 --- a/codes/csharp/chapter_searching/binary_search_edge.cs +++ /dev/null @@ -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); - } -} diff --git a/codes/dart/chapter_searching/binary_search_edge.dart b/codes/dart/chapter_searching/binary_search_edge.dart deleted file mode 100644 index 4b248131..00000000 --- a/codes/dart/chapter_searching/binary_search_edge.dart +++ /dev/null @@ -1,50 +0,0 @@ -/** - * File: binary_search_edge.dart - * Created Time: 2023-06-01 - * Author: liuyuxin (gvenusleo@gmail.com) - */ - -/* 二分查找最左一个元素 */ -int binarySearchLeftEdge(List 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 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 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"); -} diff --git a/codes/go/chapter_searching/binary_search_edge.go b/codes/go/chapter_searching/binary_search_edge.go deleted file mode 100644 index 8a27a626..00000000 --- a/codes/go/chapter_searching/binary_search_edge.go +++ /dev/null @@ -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 -} diff --git a/codes/go/chapter_searching/binary_search_test.go b/codes/go/chapter_searching/binary_search_test.go index 74d10505..0b6084d6 100644 --- a/codes/go/chapter_searching/binary_search_test.go +++ b/codes/go/chapter_searching/binary_search_test.go @@ -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) -} diff --git a/codes/java/chapter_searching/binary_search_edge.java b/codes/java/chapter_searching/binary_search_edge.java index 847a41e8..c23b655e 100644 --- a/codes/java/chapter_searching/binary_search_edge.java +++ b/codes/java/chapter_searching/binary_search_edge.java @@ -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); + } } } diff --git a/codes/java/chapter_searching/binary_search_insertion.java b/codes/java/chapter_searching/binary_search_insertion.java new file mode 100644 index 00000000..cacf553a --- /dev/null +++ b/codes/java/chapter_searching/binary_search_insertion.java @@ -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); + } + } +} diff --git a/codes/javascript/chapter_searching/binary_search_edge.js b/codes/javascript/chapter_searching/binary_search_edge.js deleted file mode 100644 index 4c7aadb9..00000000 --- a/codes/javascript/chapter_searching/binary_search_edge.js +++ /dev/null @@ -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); diff --git a/codes/python/chapter_searching/binary_search_edge.py b/codes/python/chapter_searching/binary_search_edge.py index 83e6f1c9..55ae9b8a 100644 --- a/codes/python/chapter_searching/binary_search_edge.py +++ b/codes/python/chapter_searching/binary_search_edge.py @@ -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}") diff --git a/codes/python/chapter_searching/binary_search_insertion.py b/codes/python/chapter_searching/binary_search_insertion.py new file mode 100644 index 00000000..3d0ef4d8 --- /dev/null +++ b/codes/python/chapter_searching/binary_search_insertion.py @@ -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}") diff --git a/codes/rust/chapter_searching/binary_search_edge.rs b/codes/rust/chapter_searching/binary_search_edge.rs deleted file mode 100644 index 3d6048bc..00000000 --- a/codes/rust/chapter_searching/binary_search_edge.rs +++ /dev/null @@ -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); -} \ No newline at end of file diff --git a/codes/swift/chapter_searching/binary_search_edge.swift b/codes/swift/chapter_searching/binary_search_edge.swift deleted file mode 100644 index 504f84f4..00000000 --- a/codes/swift/chapter_searching/binary_search_edge.swift +++ /dev/null @@ -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)") - } -} diff --git a/codes/typescript/chapter_searching/binary_search_edge.ts b/codes/typescript/chapter_searching/binary_search_edge.ts deleted file mode 100644 index a47bee09..00000000 --- a/codes/typescript/chapter_searching/binary_search_edge.ts +++ /dev/null @@ -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); diff --git a/docs/chapter_searching/binary_search.assets/binary_search_example.png b/docs/chapter_searching/binary_search.assets/binary_search_example.png new file mode 100644 index 00000000..de3492b2 Binary files /dev/null and b/docs/chapter_searching/binary_search.assets/binary_search_example.png differ diff --git a/docs/chapter_searching/binary_search.assets/binary_search_ranges.png b/docs/chapter_searching/binary_search.assets/binary_search_ranges.png index 5456be49..387587aa 100644 Binary files a/docs/chapter_searching/binary_search.assets/binary_search_ranges.png and b/docs/chapter_searching/binary_search.assets/binary_search_ranges.png differ diff --git a/docs/chapter_searching/binary_search.assets/binary_search_step0.png b/docs/chapter_searching/binary_search.assets/binary_search_step0.png deleted file mode 100644 index 1be714bb..00000000 Binary files a/docs/chapter_searching/binary_search.assets/binary_search_step0.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search.assets/binary_search_step1.png b/docs/chapter_searching/binary_search.assets/binary_search_step1.png index 82ec2de4..f7c27367 100644 Binary files a/docs/chapter_searching/binary_search.assets/binary_search_step1.png and b/docs/chapter_searching/binary_search.assets/binary_search_step1.png differ diff --git a/docs/chapter_searching/binary_search.assets/binary_search_step2.png b/docs/chapter_searching/binary_search.assets/binary_search_step2.png index 5e971c42..7da591dd 100644 Binary files a/docs/chapter_searching/binary_search.assets/binary_search_step2.png and b/docs/chapter_searching/binary_search.assets/binary_search_step2.png differ diff --git a/docs/chapter_searching/binary_search.assets/binary_search_step3.png b/docs/chapter_searching/binary_search.assets/binary_search_step3.png index 429a9892..f653e912 100644 Binary files a/docs/chapter_searching/binary_search.assets/binary_search_step3.png and b/docs/chapter_searching/binary_search.assets/binary_search_step3.png differ diff --git a/docs/chapter_searching/binary_search.assets/binary_search_step4.png b/docs/chapter_searching/binary_search.assets/binary_search_step4.png index 86820ed3..3c63d109 100644 Binary files a/docs/chapter_searching/binary_search.assets/binary_search_step4.png and b/docs/chapter_searching/binary_search.assets/binary_search_step4.png differ diff --git a/docs/chapter_searching/binary_search.assets/binary_search_step5.png b/docs/chapter_searching/binary_search.assets/binary_search_step5.png index 5e4855ef..0477a640 100644 Binary files a/docs/chapter_searching/binary_search.assets/binary_search_step5.png and b/docs/chapter_searching/binary_search.assets/binary_search_step5.png differ diff --git a/docs/chapter_searching/binary_search.assets/binary_search_step6.png b/docs/chapter_searching/binary_search.assets/binary_search_step6.png index 6de8cd60..7f348ec2 100644 Binary files a/docs/chapter_searching/binary_search.assets/binary_search_step6.png and b/docs/chapter_searching/binary_search.assets/binary_search_step6.png differ diff --git a/docs/chapter_searching/binary_search.assets/binary_search_step7.png b/docs/chapter_searching/binary_search.assets/binary_search_step7.png index 1b8f0fe4..196f5b1e 100644 Binary files a/docs/chapter_searching/binary_search.assets/binary_search_step7.png and b/docs/chapter_searching/binary_search.assets/binary_search_step7.png differ diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index a8991b64..11c8d879 100755 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -6,7 +6,9 @@ 给定一个长度为 $n$ 的数组 `nums` ,元素按从小到大的顺序排列,数组不包含重复元素。请查找并返回元素 `target` 在该数组中的索引。若数组不包含该元素,则返回 $-1$ 。 -对于上述问题,我们先初始化指针 $i = 0$ 和 $j = n - 1$ ,分别指向数组首元素和尾元素,代表搜索区间 $[0, n - 1]$ 。其中,中括号表示“闭区间”,即包含边界值本身。 +![二分查找示例数据](binary_search.assets/binary_search_example.png) + +对于上述问题,我们先初始化指针 $i = 0$ 和 $j = n - 1$ ,分别指向数组首元素和尾元素,代表搜索区间 $[0, n - 1]$ 。请注意,中括号表示闭区间,其包含边界值本身。 接下来,循环执行以下两个步骤: @@ -18,9 +20,6 @@ 若数组不包含目标元素,搜索区间最终会缩小为空。此时返回 $-1$ 。 -=== "<0>" - ![二分查找步骤](binary_search.assets/binary_search_step0.png) - === "<1>" ![binary_search_step1](binary_search.assets/binary_search_step1.png) diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_edge_by_element.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_edge_by_element.png new file mode 100644 index 00000000..19efc929 Binary files /dev/null and b/docs/chapter_searching/binary_search_edge.assets/binary_search_edge_by_element.png differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_naive.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_naive.png deleted file mode 100644 index b5cc56fe..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_naive.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step1.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step1.png deleted file mode 100644 index 87b5dfa7..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step1.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step2.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step2.png deleted file mode 100644 index 3d1d5f8f..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step2.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step3.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step3.png deleted file mode 100644 index 1b52576e..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step3.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step4.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step4.png deleted file mode 100644 index bc4fbc43..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step4.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step5.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step5.png deleted file mode 100644 index 9b38e4d0..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step5.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step6.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step6.png deleted file mode 100644 index 588625f7..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step6.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step7.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step7.png deleted file mode 100644 index 930ad75a..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step7.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step8.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step8.png deleted file mode 100644 index 184257cf..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_edge_step8.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_right_edge.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_left_right_edge.png deleted file mode 100644 index 06949b88..00000000 Binary files a/docs/chapter_searching/binary_search_edge.assets/binary_search_left_right_edge.png and /dev/null differ diff --git a/docs/chapter_searching/binary_search_edge.assets/binary_search_right_edge_by_left_edge.png b/docs/chapter_searching/binary_search_edge.assets/binary_search_right_edge_by_left_edge.png new file mode 100644 index 00000000..40d717a4 Binary files /dev/null and b/docs/chapter_searching/binary_search_edge.assets/binary_search_right_edge_by_left_edge.png differ diff --git a/docs/chapter_searching/binary_search_edge.md b/docs/chapter_searching/binary_search_edge.md index 9ac6addf..4641efa2 100644 --- a/docs/chapter_searching/binary_search_edge.md +++ b/docs/chapter_searching/binary_search_edge.md @@ -1,56 +1,19 @@ # 二分查找边界 -在上一节中,题目规定数组中所有元素都是唯一的。如果目标元素在数组中多次出现,上节介绍的方法只能保证返回其中一个目标元素的索引,**而无法确定该索引的左边和右边还有多少目标元素**。 +## 查找左边界 !!! question - 给定一个长度为 $n$ 的有序数组 `nums` ,数组可能包含重复元素。请查找并返回元素 `target` 在数组中首次出现的索引。若数组中不包含该元素,则返回 $-1$ 。 + 给定一个长度为 $n$ 的有序数组 `nums` ,数组可能包含重复元素。请返回数组中最左一个元素 `target` 的索引。若数组中不包含该元素,则返回 $-1$ 。 -## 线性方法 +回忆二分查找插入点的方法,搜索完成后,$i$ 指向最左一个 `target` ,**因此查找插入点本质上是在查找最左一个 `target` 的索引**。 -为了查找数组中最左边的 `target` ,我们可以分为两步: +考虑通过查找插入点的函数实现查找左边界。请注意,数组中可能不包含 `target` ,此时有两种可能: -1. 进行二分查找,定位到任意一个 `target` 的索引,记为 $k$ 。 -2. 以索引 $k$ 为起始点,向左进行线性遍历,找到最左边的 `target` 返回即可。 +1. 插入点的索引 $i$ 越界; +2. 元素 `nums[i]` 与 `target` 不相等; -![线性查找最左边的元素](binary_search_edge.assets/binary_search_left_edge_naive.png) - -这个方法虽然有效,但由于包含线性查找,时间复杂度为 $O(n)$ ,当存在很多重复的 `target` 时效率较低。 - -## 二分方法 - -考虑仅使用二分查找解决该问题。整体算法流程不变,先计算中点索引 $m$ ,再判断 `target` 和 `nums[m]` 大小关系: - -- 当 `nums[m] < target` 或 `nums[m] > target` 时,说明还没有找到 `target` ,因此采取与上节代码相同的缩小区间操作,**从而使指针 $i$ 和 $j$ 向 `target` 靠近**。 -- 当 `nums[m] == target` 时,说明“小于 `target` 的元素”在区间 $[i, m - 1]$ 中,因此采用 $j = m - 1$ 来缩小区间,**从而使指针 $j$ 向小于 `target` 的元素靠近**。 - -二分查找完成后,**$i$ 指向最左边的 `target` ,$j$ 指向首个小于 `target` 的元素**,因此返回索引 $i$ 即可。 - -=== "<1>" - ![二分查找最左边元素的步骤](binary_search_edge.assets/binary_search_left_edge_step1.png) - -=== "<2>" - ![binary_search_left_edge_step2](binary_search_edge.assets/binary_search_left_edge_step2.png) - -=== "<3>" - ![binary_search_left_edge_step3](binary_search_edge.assets/binary_search_left_edge_step3.png) - -=== "<4>" - ![binary_search_left_edge_step4](binary_search_edge.assets/binary_search_left_edge_step4.png) - -=== "<5>" - ![binary_search_left_edge_step5](binary_search_edge.assets/binary_search_left_edge_step5.png) - -=== "<6>" - ![binary_search_left_edge_step6](binary_search_edge.assets/binary_search_left_edge_step6.png) - -=== "<7>" - ![binary_search_left_edge_step7](binary_search_edge.assets/binary_search_left_edge_step7.png) - -=== "<8>" - ![binary_search_left_edge_step8](binary_search_edge.assets/binary_search_left_edge_step8.png) - -注意,数组可能不包含目标元素 `target` 。因此在函数返回前,我们需要先判断 `nums[i]` 与 `target` 是否相等,以及索引 $i$ 是否越界。 +当遇到以上两种情况时,直接返回 $-1$ 即可。 === "Java" @@ -126,9 +89,19 @@ ## 查找右边界 -类似地,我们也可以二分查找最右边的 `target` 。当 `nums[m] == target` 时,说明大于 `target` 的元素在区间 $[m + 1, j]$ 中,因此执行 `i = m + 1` ,**使得指针 $i$ 向大于 `target` 的元素靠近**。 +那么如何查找最右一个 `target` 呢?最直接的方式是修改代码,替换在 `nums[m] == target` 情况下的指针收缩操作。代码在此省略,有兴趣的同学可以自行实现。 -完成二分后,**$i$ 指向首个大于 `target` 的元素,$j$ 指向最右边的 `target`** ,因此返回索引 $j$ 即可。 +下面我们介绍两种更加取巧的方法。 + +### 复用查找左边界 + +实际上,我们可以利用查找最左元素的函数来查找最右元素,具体方法为:**将查找最右一个 `target` 转化为查找最左一个 `target + 1`**。 + +查找完成后,指针 $i$ 指向最左一个 `target + 1`(如果存在),而 $j$ 指向最右一个 `target` ,**因此返回 $j$ 即可**。 + +![将查找右边界转化为查找左边界](binary_search_edge.assets/binary_search_right_edge_by_left_edge.png) + +请注意,返回的插入点是 $i$ ,因此需要将其减 $1$ ,从而获得 $j$ 。 === "Java" @@ -202,10 +175,18 @@ [class]{}-[func]{binary_search_right_edge} ``` -观察下图,搜索最右边元素时指针 $j$ 的作用与搜索最左边元素时指针 $i$ 的作用一致,反之亦然。也就是说,**搜索最左边元素和最右边元素的实现是镜像对称的**。 +### 转化为查找元素 -![查找最左边和最右边元素的对称性](binary_search_edge.assets/binary_search_left_right_edge.png) +我们知道,当数组不包含 `target` 时,最后 $i$ , $j$ 会分别指向首个大于、小于 `target` 的元素。 -!!! tip +根据上述结论,我们可以构造一个数组中不存在的元素,用于查找左右边界: - 以上代码采取的都是“双闭区间”写法。有兴趣的读者可以自行实现“左闭右开”写法。 +- 查找最左一个 `target` :可以转化为查找 `target - 0.5` ,并返回指针 $i$ 。 +- 查找最右一个 `target` :可以转化为查找 `target + 0.5` ,并返回指针 $j$ 。 + +![将查找边界转化为查找元素](binary_search_edge.assets/binary_search_edge_by_element.png) + +代码在此省略,值得注意的有: + +- 给定数组不包含小数,这意味着我们无需关心如何处理相等的情况。 +- 因为该方法引入了小数,所以需要将函数中的变量 `target` 改为浮点数类型。 diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_example.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_example.png new file mode 100644 index 00000000..b4a56b9e Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_example.png differ diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_naive.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_naive.png new file mode 100644 index 00000000..652fe5b4 Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_naive.png differ diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step1.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step1.png new file mode 100644 index 00000000..d5c31ab8 Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step1.png differ diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step2.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step2.png new file mode 100644 index 00000000..7148767b Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step2.png differ diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step3.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step3.png new file mode 100644 index 00000000..08642a49 Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step3.png differ diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step4.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step4.png new file mode 100644 index 00000000..7d5d0715 Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step4.png differ diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step5.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step5.png new file mode 100644 index 00000000..af2952c1 Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step5.png differ diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step6.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step6.png new file mode 100644 index 00000000..afc8e5b9 Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step6.png differ diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step7.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step7.png new file mode 100644 index 00000000..32595e2f Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step7.png differ diff --git a/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step8.png b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step8.png new file mode 100644 index 00000000..ea004528 Binary files /dev/null and b/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_step8.png differ diff --git a/docs/chapter_searching/binary_search_insertion.md b/docs/chapter_searching/binary_search_insertion.md new file mode 100644 index 00000000..31ca1935 --- /dev/null +++ b/docs/chapter_searching/binary_search_insertion.md @@ -0,0 +1,227 @@ +# 二分查找插入点 + +二分查找不仅可用于搜索目标元素,还具有许多变种问题,比如搜索目标元素的插入位置。 + +## 无重复元素的情况 + +!!! question + + 给定一个长度为 $n$ 的有序数组 `nums` 和一个元素 `target` ,数组不存在重复元素。现将 `target` 插入到数组 `nums` 中,并保持其有序性。若数组中已存在元素 `target` ,则插入到其左方。请返回插入后 `target` 在数组中的索引。 + +![二分查找插入点示例数据](binary_search_insertion.assets/binary_search_insertion_example.png) + +如果想要复用上节的二分查找代码,则需要回答以下两个问题。 + +**问题一**:当数组中包含 `target` 时,插入点的索引是否是该元素的索引? + +题目要求将 `target` 插入到相等元素的左边,这意味着新插入的 `target` 替换了原来 `target` 的位置。也就是说,**当数组包含 `target` 时,插入点的索引就是该 `target` 的索引**。 + +**问题二**:当数组中不存在 `target` 时,插入点是哪个元素的索引? + +进一步思考二分查找过程:当 `nums[m] < target` 时 $i$ 移动,这意味着指针 $i$ 在向大于等于 `target` 的元素靠近。同理,指针 $j$ 始终在向小于等于 `target` 的元素靠近。 + +因此二分结束时一定有:$i$ 指向首个大于 `target` 的元素,$j$ 指向首个小于 `target` 的元素。**易得当数组不包含 `target` 时,插入索引为 $i$** 。 + +=== "Java" + + ```java title="binary_search_insertion.java" + [class]{binary_search_insertion}-[func]{binarySearchInsertionSimple} + ``` + +=== "C++" + + ```cpp title="binary_search_insertion.cpp" + [class]{}-[func]{binarySearchInsertionSimple} + ``` + +=== "Python" + + ```python title="binary_search_insertion.py" + [class]{}-[func]{binary_search_insertion_simple} + ``` + +=== "Go" + + ```go title="binary_search_insertion.go" + [class]{}-[func]{binarySearchInsertionSimple} + ``` + +=== "JS" + + ```javascript title="binary_search_insertion.js" + [class]{}-[func]{binarySearchInsertionSimple} + ``` + +=== "TS" + + ```typescript title="binary_search_insertion.ts" + [class]{}-[func]{binarySearchInsertionSimple} + ``` + +=== "C" + + ```c title="binary_search_insertion.c" + [class]{}-[func]{binarySearchInsertionSimple} + ``` + +=== "C#" + + ```csharp title="binary_search_insertion.cs" + [class]{binary_search_insertion}-[func]{binarySearchInsertionSimple} + ``` + +=== "Swift" + + ```swift title="binary_search_insertion.swift" + [class]{}-[func]{binarySearchInsertionSimple} + ``` + +=== "Zig" + + ```zig title="binary_search_insertion.zig" + [class]{}-[func]{binarySearchInsertionSimple} + ``` + +=== "Dart" + + ```dart title="binary_search_insertion.dart" + [class]{}-[func]{binarySearchInsertionSimple} + ``` + +=== "Rust" + + ```rust title="binary_search_insertion.rs" + [class]{}-[func]{binary_search_insertion} + ``` + +## 存在重复元素的情况 + +!!! question + + 在上一题的基础上,规定数组可能包含重复元素,其余不变。 + +假设数组中存在多个 `target` ,则普通二分查找只能返回其中一个 `target` 的索引,**而无法确定该元素的左边和右边还有多少 `target`**。 + +题目要求将目标元素插入到最左边,**所以我们需要查找数组中最左一个 `target` 的索引**。初步考虑通过以下两步实现: + +1. 执行二分查找,得到任意一个 `target` 的索引,记为 $k$ 。 +2. 从索引 $k$ 开始,向左进行线性遍历,当找到最左边的 `target` 时返回。 + +![线性查找重复元素的插入点](binary_search_insertion.assets/binary_search_insertion_naive.png) + +此方法虽然可用,但其包含线性查找,因此时间复杂度为 $O(n)$ 。当数组中存在很多重复的 `target` 时,该方法效率很低。 + +现考虑修改二分查找代码。整体流程不变,每轮先计算中点索引 $m$ ,再判断 `target` 和 `nums[m]` 大小关系: + +1. 当 `nums[m] < target` 或 `nums[m] > target` 时,说明还没有找到 `target` ,因此采用普通二分查找的缩小区间操作,**从而使指针 $i$ 和 $j$ 向 `target` 靠近**。 +2. 当 `nums[m] == target` 时,说明小于 `target` 的元素在区间 $[i, m - 1]$ 中,因此采用 $j = m - 1$ 来缩小区间,**从而使指针 $j$ 向小于 `target` 的元素靠近**。 + +循环完成后,$i$ 指向最左边的 `target` ,$j$ 指向首个小于 `target` 的元素,**因此索引 $i$ 就是插入点**。 + +=== "<1>" + ![二分查找重复元素的插入点的步骤](binary_search_insertion.assets/binary_search_insertion_step1.png) + +=== "<2>" + ![binary_search_insertion_step2](binary_search_insertion.assets/binary_search_insertion_step2.png) + +=== "<3>" + ![binary_search_insertion_step3](binary_search_insertion.assets/binary_search_insertion_step3.png) + +=== "<4>" + ![binary_search_insertion_step4](binary_search_insertion.assets/binary_search_insertion_step4.png) + +=== "<5>" + ![binary_search_insertion_step5](binary_search_insertion.assets/binary_search_insertion_step5.png) + +=== "<6>" + ![binary_search_insertion_step6](binary_search_insertion.assets/binary_search_insertion_step6.png) + +=== "<7>" + ![binary_search_insertion_step7](binary_search_insertion.assets/binary_search_insertion_step7.png) + +=== "<8>" + ![binary_search_insertion_step8](binary_search_insertion.assets/binary_search_insertion_step8.png) + +观察以下代码,判断分支 `nums[m] > target` 和 `nums[m] == target` 的操作相同,因此两者可以合并。 + +即便如此,我们仍然可以将判断条件保持展开,因为其逻辑更加清晰、可读性更好。 + +=== "Java" + + ```java title="binary_search_insertion.java" + [class]{binary_search_insertion}-[func]{binarySearchInsertion} + ``` + +=== "C++" + + ```cpp title="binary_search_insertion.cpp" + [class]{}-[func]{binarySearchInsertion} + ``` + +=== "Python" + + ```python title="binary_search_insertion.py" + [class]{}-[func]{binary_search_insertion} + ``` + +=== "Go" + + ```go title="binary_search_insertion.go" + [class]{}-[func]{binarySearchInsertion} + ``` + +=== "JS" + + ```javascript title="binary_search_insertion.js" + [class]{}-[func]{binarySearchInsertion} + ``` + +=== "TS" + + ```typescript title="binary_search_insertion.ts" + [class]{}-[func]{binarySearchInsertion} + ``` + +=== "C" + + ```c title="binary_search_insertion.c" + [class]{}-[func]{binarySearchInsertion} + ``` + +=== "C#" + + ```csharp title="binary_search_insertion.cs" + [class]{binary_search_insertion}-[func]{binarySearchInsertion} + ``` + +=== "Swift" + + ```swift title="binary_search_insertion.swift" + [class]{}-[func]{binarySearchInsertion} + ``` + +=== "Zig" + + ```zig title="binary_search_insertion.zig" + [class]{}-[func]{binarySearchInsertion} + ``` + +=== "Dart" + + ```dart title="binary_search_insertion.dart" + [class]{}-[func]{binarySearchInsertion} + ``` + +=== "Rust" + + ```rust title="binary_search_insertion.rs" + [class]{}-[func]{binary_search_insertion} + ``` + +!!! tip + + 本节的代码都是“双闭区间”写法。有兴趣的读者可以自行实现“左闭右开”写法。 + +总的来看,二分查找无非就是给指针 $i$ , $j$ 分别设定搜索目标,目标可能是一个具体的元素(例如 `target` ),也可能是一个元素范围(例如小于 `target` 的元素)。 + +在不断的循环二分中,指针 $i$ , $j$ 都逐渐逼近预先设定的目标。最终,它们或是成功找到答案,或是越过边界后停止。 diff --git a/mkdocs.yml b/mkdocs.yml index c7a6d3eb..0ee62480 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -207,10 +207,13 @@ nav: # [icon: material/text-search] - chapter_searching/index.md - 10.1.   二分查找: chapter_searching/binary_search.md - - 10.2.   二分查找边界: chapter_searching/binary_search_edge.md - - 10.3.   哈希优化策略: chapter_searching/replace_linear_by_hashing.md - - 10.4.   重识搜索算法: chapter_searching/searching_algorithm_revisited.md - - 10.5.   小结: chapter_searching/summary.md + # [status: new] + - 10.2.   二分查找插入点: chapter_searching/binary_search_insertion.md + # [status: new] + - 10.3.   二分查找边界: chapter_searching/binary_search_edge.md + - 10.4.   哈希优化策略: chapter_searching/replace_linear_by_hashing.md + - 10.5.   重识搜索算法: chapter_searching/searching_algorithm_revisited.md + - 10.6.   小结: chapter_searching/summary.md - 11.   排序: # [icon: material/sort-ascending] - chapter_sorting/index.md