-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution34.java
More file actions
38 lines (36 loc) · 1.16 KB
/
Solution34.java
File metadata and controls
38 lines (36 loc) · 1.16 KB
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
/**
* Created by alex on 16-2-23.
*/
//Given [5, 7, 7, 8, 8, 10] and target value 8,
// return [3, 4].
public class Solution34 {
public int[] searchRange(int[] nums, int target) {
int [] result = new int[2];
search(nums, target, 0, nums.length-1, result);
return result;
}
public void search(int[] nums, int target, int left, int right, int[] result){
if(left < right){
int mid = (left + right) / 2;
if(nums[mid] < target){
search(nums, target, mid+1, right, result);
}else if(nums[mid] > target){
search(nums, target, left, mid, result);
}else {
result[0] = result[1] = mid;
while(result[0] >0 && nums[result[0]-1] == target){
result[0]--;
}
while(result[1] < nums.length-1 && nums[result[1] + 1] == target){
result[1]++;
}
}
}else {
if(nums[left] == target){
result[0] = result[1] = left;
}else {
result[0] = result[1] = -1;
}
}
}
}