mirror of
https://gitee.com/TheAlgorithms/LeetCodeAnimation.git
synced 2024-12-06 15:19:44 +08:00
21 lines
510 B
Java
Executable File
21 lines
510 B
Java
Executable File
//时间复杂度:O(lon(n))
|
||
//空间复杂度:O(1)
|
||
class Solution2 {
|
||
public int searchInsert(int[] nums, int target) {
|
||
if (target>nums[nums.length-1]) {
|
||
return nums.length;
|
||
}
|
||
int left=0;
|
||
int right=nums.length-1;
|
||
while (left < right) {
|
||
int mid = (left + right) / 2;
|
||
if (nums[mid] < target) {
|
||
left = mid + 1;
|
||
} else {
|
||
right = mid;
|
||
}
|
||
}
|
||
return left;
|
||
|
||
}
|
||
} |