这就是相当没意思了吧
全都是二分法,我觉得可能数组最难就这了?
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
int mid = (left + right) >> 1;
while (left <= right) {
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
mid = (left + right) >> 1;
} else {
left = mid + 1;
mid = (left + right) >> 1;
}
}
return mid+1;
}
}
一遍过