题解 | #二分查找-II#
二分查找-II
http://www.nowcoder.com/practice/4f470d1d3b734f8aaf2afb014185b395
就是为了单纯的记录,
public int search (int[] nums, int target) {
// write code here
int low = 0, high = nums.length, mid=0;
if (high == 0){
return -1;
}
while(low <= high){
mid = (low + high) / 2;
if(nums[mid] == target){
break;
}else if(nums[mid] > target){
high = mid - 1;
}else{
low = mid + 1;
}
}
if(low > high){
return -1;
}
if(mid == 0){
return mid;
}
int x = mid - 1; while( x>= 0 && nums[x] == nums[mid]){ mid = x; x--; } return mid; }