public static int searchInsert(int[] nums, int target) {
if (nums[0] > target) {
return 0;
}
if (nums[(nums.length - 1)] < target) {
return nums.length;
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
return i;
} else if (nums[i] < target) {
// i <target<i+1
if (nums[i + 1] > target) {
return i + 1;
}
} else {
// i-1 < target < i
if (nums[i - 1] < target) {
return i;
}
}
}
return -1;
}
还可以采用二分法