入门二分,注意找到一个值后进行左右方向寻找边界问题。ac代码为:
public int[] searchRange(int[] nums, int target) {
int a[]= {-1,-1};
if(nums.length==1&&nums[0]==target) {a[0]=0;a[1]=0;return a;}
if(nums.length==0)return a;
int leftindex,rightindex;
int left=0,right=nums.length-1;
while (left<right) {
//System.out.println(left+" "+right);
int mid=(left+right)/2;
if(nums[mid]==target)
{
leftindex=mid;
rightindex=mid;
while (leftindex>=0&&nums[leftindex]==target) {leftindex--;}
while (rightindex<nums.length&&nums[rightindex]==target) {rightindex++;}
a[0]=leftindex+1;a[1]=rightindex-1;
return a;
}
else if (nums[mid]<target) {
left=mid+1;
}
else {
right=mid;
}
}
if(nums[left]==target)
{
a[0]=left;a[1]=left;
}
return a;
}