classSolution { public: vector<int>searchRange(vector<int>&nums, inttarget) { if(nums.empty()) return {-1,-1}; intl=0, r=nums.size() -1; //二分范围while( l<r) //查找元素的开始位置 { intmid= (l+r )/2; if(nums[mid] >=target) r=mid; elsel=mid+1; } if( nums[r] !=target) return {-1,-1}; //查找失败intL=r; l=0, r=nums.size() -1; //二分范围while( l<r) //查找元素的结束位置 { intmid= (l+r+1)/2; if(nums[mid] <=target ) l=mid; elser=mid-1; } return {L,r}; } };