算法算法LeetCode二分查找重言2023-07-192023-07-1912345678910111213141516171819public int binarySearch(int[] nums, int target) { // 二分查找 int left = 0, right = nums.length - 1; int mid = 0; while (left <= right) { mid = left + ((right - left) >> 1); if (nums[mid] == target) { return mid; } // 二分查找的模板 if (nums[mid] < target) { left = mid + 1; } else if (nums[mid] > target) { right = mid - 1; } } return left; }