-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
32 lines (32 loc) · 952 Bytes
/
Solution.java
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
31
32
class Solution {
public int[] searchRange(int[] nums, int target) {
int head = 0;
int tail = nums.length - 1;
int[] res = new int[2];
int mid = (head + tail) / 2;
while(head <= tail) {
mid = (head + tail) / 2;
if(nums[mid] > target) tail = mid - 1;
else if(nums[mid] < target) head = mid + 1;
else break;
}
if(head <= tail) { // find the target
head = mid;
tail = mid;
while(head >= 0) {
if(nums[head] == target) head--;
else break;
}
while(tail <= nums.length - 1) {
if(nums[tail] == target) tail++;
else break;
}
res[0] = head + 1;
res[1] = tail - 1;
} else { // did not find the target
res[0] = -1;
res[1] = -1;
}
return res;
}
}