public class Solution {
public int[] searchRange(int[] A, int target) {
int[] res = new int[2];
res[0] = Integer.MAX_VALUE;
res[1] = Integer.MIN_VALUE;
range(A, target, 0, A.length-1, res);
if (res[0]==Integer.MAX_VALUE||res[1]==Integer.MIN_VALUE) {
res[0] = -1;
res[1] = -1;
}
return res;
}
public void range(int[] A, int target, int low, int high, int[] res) {
if (low>high) return;
int mid = (low+high)/2;
if (A[mid]==target) {
res[0] = Math.min(res[0], mid);
res[1] = Math.max(res[1], mid);
range(A, target, low, mid-1, res);
range(A, target, mid+1, high, res);
} else if (A[mid]<target) range(A, target, mid+1, high, res);
else range(A, target, low, mid-1, res);
}
}
没有评论:
发表评论