public class BiSearch { public static void main(String[] args) { int nums[] = {-1,0,3,5,9,12}; System.out.println(search(nums, 9)); } public static int search(int[] nums, int target) { int low = 0,high = nums.length - 1; while(low <= high){ int mid = (low+high)/2; if(nums[mid] == target) return mid; else if(nums[mid] > target) high = mid - 1; else low = mid + 1; } return -1; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)