Search Insert Position
LeetCode https://leetcode.cn/problems/search-insert-position/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int searchInsert(int[] nums, int target) {
// if (nums.length <= 1) {
// return 1;
// }
int left = 0;
int right = nums.length - 1;
int mid = left + (right - left) / 2;
while (left <= right) {
if (nums[mid] == target) {
return mid;
}
if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
mid = left + (right - left) / 2;
}
return mid;
}
}
考虑 nums = [1], target = 0 的情况!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int searchInsert(int[] nums, int target) {
int n = nums.length;
int left = 0, right = n - 1, ans = n;
while (left <= right) {
int mid = ((right - left) >> 1) + left;
if (target <= nums[mid]) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
}
Complexity
- Time = O(logn)
- Space = O(1)
This post is licensed under CC BY 4.0 by the author.