guol
二分查找
http://www.nowcoder.com/questionTerminal/4f470d1d3b734f8aaf2afb014185b395
public int search (int[] nums, int target) { // write code here int left=0; int right=nums.length; if(nums.length==0) { return -1; } boolean flag=false; while(left<right) { int mid=left+(right-left)/2; if(nums[mid]==target) { flag=true; right=mid; } else if(nums[mid]<target) { left=mid+1; } else if(nums[mid]>target) { right=mid; } } if(flag==true) return left; else return -1; }
}