Online to see better ideas, Online said is, first use the dichotomy to find the left point, and then use a binary search to find the right breakpoint. The problem is solved.
My thinking is not very good, I was the first to find the index equal to target, and then the center to the two sides of the expansion.
classSolution { Public: Vector<int> Searchrange (vector<int>& Nums,inttarget) { intlen=nums.size (); intL=0, r=len-1; intmid; Vector<int>Res; intflag=0; while(l<=r) {Mid=l+ (r-l)/2; if(nums[mid]<target) L=mid+1; Else if(nums[mid]>target) R=mid-1; Else{flag=1; Break; } } if(flag==0) {Res.push_back (-1); Res.push_back (-1); } Else{L=mid; R=mid; while(l>=0&&nums[l]==target) { if(nums[l]==target) L--; } while(r<=len-1&&nums[r]==target) { if(nums[r]==target) R++; } res.push_back (L+1); Res.push_back (R-1); } returnRes; }};
Search for a Range