public class _704二分查找 {
public static int search(int[] nums, int target) {
if (null == nums || nums.length == 0) {
return -1;
}
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
} else if (nums[mid] < target) {
left = mid + 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] nums = {1, 1, 7, 10, 30};
int search = search(nums, 7);
System.out.println(search);
}
}
二分查找【标准二分查找】
最新推荐文章于 2024-08-18 21:36:31 发布