二分,太简单,没啥好说的
class Solution {
public int searchInsert(int[] nums, int target) {
if (nums.length == 0) return 0;
int i = 0, j = nums.length;
int mid = (i + j) / 2;
while (i < j) {
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
if (j == mid) break;
j = mid;
mid = (i + j) / 2;
} else {
if (i == mid) break;
i = mid;
mid = (i + j) / 2;
}
}
return j;
}
}
原文地址:https://www.cnblogs.com/acbingo/p/9302534.html
时间: 2024-10-27 17:04:25