given an array that contains no duplicate value, and a target value, search the insert position this target should be.
it’s just a regular binary search.
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 (nums[mid] == target) return mid;
if (target < nums[mid]) right = mid - 1;
else left = mid + 1;
}
return left; //left will stopped at the first one which large than target
}
}
but what if the given array contains duplicate value?