-
Notifications
You must be signed in to change notification settings - Fork 5
/
FirstAndLastPositions.java
39 lines (38 loc) · 1.15 KB
/
FirstAndLastPositions.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
33
34
35
36
37
38
39
/*https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/*/
class Solution {
public int[] searchRange(int[] nums, int target) {
int[] res = new int[2];
res[0] = res[1] = -1;
res[0] = findLeftMost(nums,target,0,nums.length-1);
res[1] = findRightMost(nums,target,0,nums.length-1);
return res;
}
public int findRightMost(int[] a, int t, int s, int e)
{
if (s <= e)
{
int m = (s+e)/2;
if (a[m] == t && (m == a.length-1 || a[m+1] != t))
return m;
else if (a[m] > t)
return findRightMost(a,t,s,m-1);
else if (a[m] <= t)
return findRightMost(a,t,m+1,e);
}
return -1;
}
public int findLeftMost(int[] a, int t, int s, int e)
{
if (s <= e)
{
int m = (s+e)/2;
if (a[m] == t && (m == 0 || a[m-1] != t))
return m;
else if (a[m] >= t)
return findLeftMost(a,t,s,m-1);
else if (a[m] < t)
return findLeftMost(a,t,m+1,e);
}
return -1;
}
}