1,二分:
public static int binarySearch(int[] nums, int target) { if(nums == null || nums.length == 0) { return -1; } int start = 0; int end = nums.length - 1; while (start <= end){ int mid = start + (end - start) / 2; if (nums[mid] == target){ return mid; } else if (nums[mid] < target) { start = mid + 1; } else { end = mid - 1; } } return -1; }
时间: 2024-10-26 16:38:48