35.搜索插入位置
使用二分查找:
class Solution {
public int searchInsert(int[] nums, int target) {
int low = 0,high = nums.length -1;
while(low <= high){
//注意每次循环完都要计算mid
int mid = (low + high)/2;
if(nums[mid] == target){
return mid;
}
if(nums[mid] < target){
low = mid + 1;
}
if(nums[mid] > target){
high = mid -1;
}
}
return low ;
}
}