LeetCode35二分查找

int mid = ((right - left) >> 1) + left; /2 用这个方法
right-left 就是从0开始到n, 右移动一位就是除以2,获得到这个区间的一半,加上起始的left 就是(right + left)/2
This commit is contained in:
whai 2023-12-06 21:46:19 +08:00
parent 66f8bc1019
commit 57aa0a6aa9

View File

@ -44,7 +44,8 @@ public class LeetCode35 {
int right = nums.length - 1; int right = nums.length - 1;
// 当左指针小于等于右指针时进行循环 // 当左指针小于等于右指针时进行循环
while (left <= right) { while (left <= right) {
// 计算中间位置的索引 // 计算中间位置的索引 右边距减去左边距右移一位然后加上左边距得到中间值
// right-left 就是从0开始到n 右移动一位就是除以2获得到这个区间的一半加上起始的left 就是(right + left)/2
int middle = ((right - left) >> 1) + left; int middle = ((right - left) >> 1) + left;
// 如果中间位置的元素等于目标元素返回中间位置的索引 // 如果中间位置的元素等于目标元素返回中间位置的索引
if (nums[middle] == target) { if (nums[middle] == target) {
@ -84,6 +85,9 @@ public class LeetCode35 {
int[] ints = {1, 3, 5, 8,10}; int[] ints = {1, 3, 5, 8,10};
int[] ints1 = {}; int[] ints1 = {};
int middle = ((8 - 4) >> 1) + 4;
// 1000 - 0100 = 0100 >> 1 = 0010 ==> 2 + 4
System.out.println(searchInsert1(ints, 2)); System.out.println(searchInsert1(ints, 2));
} }
} }