public class half { public static int find(int[] nums,int num){ int max=nums.length-1; int min=0; int mid=-1; while(min<=max){ mid=(max+min)/2; if(nums[mid]>num){ max=mid-1; }else if(nums[mid]<num){ min=mid+1; }else if(nums[mid]==num){ return mid; } } return -1; } public static void main(String[] args) { int[] nums={1,2,5,7,9}; System.out.println(find(nums,5)); } }
相关视频: