//二分,刚好上一题有了,这个直接用,改一下判断条件就好了...
class Solution {
public int searchInsert(int[] nums, int target){
if (nums.length == 0) return 0;
int index = searchLeft(nums, target);
return index;
}
public int searchLeft(int[] nums, int target){
int left = 0;
int right = nums.length;
int mid = 0;
while (left < right - 1){ // 左闭右开
// System.out.println(left + " " + right);
mid = (left + right) / 2;
if (target > nums[mid])
left = mid + 1;
else if (target == nums[mid])
left = mid;
else
right = mid;
}
// System.out.println("left : " + left + " " + right);
if (left < nums.length && nums[left] >= target)
return left;
return right;
}
}
LeetCode 35 搜索插入位置 二分
猜你喜欢
转载自blog.csdn.net/TIMELIMITE/article/details/89853025
今日推荐
周排行