-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ34.java
47 lines (40 loc) · 1.08 KB
/
Q34.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
package algorithms;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Q34 {
public int[] searchRange(int[] nums, int target) {
int[] ans = {-1, -1};
if (nums.length == 0) {
return ans;
}
int left = 0, right = nums.length - 1;
while (left < right) {
if (nums[left] != target) {
left++;
} else {
break;
}
}
while (right > left) {
if (nums[right] != target) {
right--;
} else {
break;
}
}
if (left == nums.length - 1 && nums[left] != target) {
return ans;
}
ans[0] = left;
ans[1] = right;
return ans;
}
public static void main(String[] args) {
int[] nums = {5, 7, 7, 8, 8, 10};
int target = 8;
Q34 q = new Q34();
int[] ans = q.searchRange(nums, target);
System.out.println(Arrays.toString(ans));
}
}