-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35.search-insert-position.java
More file actions
33 lines (30 loc) · 931 Bytes
/
Copy path35.search-insert-position.java
File metadata and controls
33 lines (30 loc) · 931 Bytes
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
33
class Solution {
public int[] searchRange(int[] nums, int target) {
int l = 0;
int r = nums.length-1;
int[] res = new int[2];
while(l <= r){
int mid = (l+r)/2;
if(nums[mid] == target)
{
//res[0] = mid;
int currmid = mid-1;
while(currmid>=0 && nums[currmid] == target)
currmid--;
res[0] = currmid+1;
currmid = mid+1;
while(currmid<nums.length && nums[currmid] == target)
currmid++;
// currmid--;
res[1] = currmid-1;
return res;
}
else if(nums[mid] > target){
r = mid-1;
}else{
l = mid+1;
}
}
return new int[]{-1,-1};
}
}