public class Solution {
public int searchInsert(int[] nums, int target) {
if (nums == null || nums.length < 1) {
return -1;
}
int length = nums.length;
int left = 0;
int right = length - 1;
while (left < right - 1) {// very useful prototype !!
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
left = mid;
} else {
right = mid;
}
}
if (target <= nums[left]) {
return left;
} else if (target <= nums[right]) {
return right;
} else {
return right + 1;
}
}
}