public class Solution { public int[] searchRange(int[] nums, int target) { int []res=new int[2]; int size=nums.length; int left=0; int right=size-1; int res1=-1; int res0=-1; while(left!=right&&left<=right) { int temp=nums[(left+right)/2]; if(temp<target) { left=(left+right)/2+1; } else { right=(left+right)/2; } } if(nums[left]==target) res0=left; left=0; right=size-1; while(left!=right&&left<=right) { int temp=nums[(left+right+1)/2]; if(temp>target) { right=(left+right+1)/2-1; } else { left=(left+right+1)/2; } } if(nums[right]==target) res1=right; res[0]=res0; res[1]=res1; return res; } }
原文:http://www.cnblogs.com/aguai1992/p/5731145.html