2023 Dec LC challenge
題目 34. Find First and Last Position of Element in Sorted Array
用兩次二分法找target最初位置以及最後一個位置
My Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| class Solution {
public int[] searchRange(int[] nums, int target) {
int start = findPosition(nums, target, true);
if (start == nums.length || nums[start] != target) {
return new int[]{-1, -1};
}
int end = findPosition(nums, target, false)-1;
//目標值的結束位置的下一個位置」調整回「目標值的實際結束位置。
return new int[]{start, end};
}
public int findPosition(int[] nums, int target, boolean findStart) {
int left = 0;
int right = nums.length;
while(left < right) {
int mid = left + (right - left)/2;
if(nums[mid] > target) {
right = mid;
} else if(nums[mid] < target) {
left = mid + 1;
} else if( findStart && nums[mid] == target) {
// 找起始位置
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
|