class Solution {
/**
* 第一个大于等于n的元素位置
* @param nums
* @param target
* @return
*/
private int lowerBound(int[] nums, int target) {
int size = nums.length;
int l = 0;
while (size > 0) {
int half = size >> 1;
int m = l + half;
if (nums[m] < target) {
size = size - half - 1;
l = m + 1;
} else {
size = half;
}
}
return l;
}
/**
* 第一个大于n的元素位置
* @param nums
* @param target
* @return
*/
private int upperBound(int[] nums, int target) {
int size = nums.length;
int l = 0;
while (size > 0) {
int half = size >> 1;
int m = l + half;
if (nums[m] <= target) {
size = size - half - 1;
l = m + 1;
} else {
size = half;
}
}
return l;
}
public int searchInsert(int[] nums, int target) {
return lowerBound(nums, target);
}
}
转载于:https://blog.51cto.com/tianyiya/2172743