-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsol.java
52 lines (47 loc) · 1.33 KB
/
sol.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
40
41
42
43
44
45
46
47
48
49
50
51
52
public class sol {
public static void main(String[] args) {
// call the fn here
}
class Solution {
public int[] searchRange(int[] nums, int target) {
int[] arr = new int[2];
arr[0] = firstIndex(nums, target);
arr[1] = lastIndex(nums, target);
return arr;
}
int firstIndex(int[] nums, int target) {
int index = -1;
int s = 0;
int e = nums.length - 1;
while (s <= e) {
int mid = s + (e - s) / 2;
if (nums[mid] >= target) {
e = mid - 1;
} else {
s = mid + 1;
}
if (nums[mid] == target) {
index = mid;
}
}
return index;
}
int lastIndex(int[] nums, int target) {
int index = -1;
int s = 0;
int e = nums.length - 1;
while (s <= e) {
int mid = s + (e - s) / 2;
if (nums[mid] <= target) {
s = mid + 1;
} else {
e = mid - 1;
}
if (nums[mid] == target) {
index = mid;
}
}
return index;
}
}
}