leetcode35.搜索插入位置
二分查找,如果目标值在数组中,那么索引就是mid,如果目标值不在数组中,那么最后他应该插入的索引是left
class Solution {public int searchInsert(int[] nums, int target) {int left = 0, right = nums.length - 1;while (left <= right) {int mid = left + (right - left) / 2;if (target == nums[mid]) {return mid;} else if (target < nums[mid]) {right = mid - 1;} else {left = mid + 1;} }return left;}
}