vector<int> searchRange(vector<int>& nums, int target) {
int len = nums.size(), i=0, j=len1, mid;
vector<int> res(2,1);
while(i<=j)
{
mid = i + (ji)/2;
if(target == nums[mid])
{
i=mid;
while(i>0 && nums[i]==nums[i1]) i;
while(mid<j && nums[mid]==nums[mid+1]) mid++;
res[0]=i;
res[1]=mid;
return res;
}
else if(target>nums[mid])
{
i = mid+1;
}
else
{
j = mid1;
}
}
return res;
}
My 12ms c++ concise solution


@KevinChan Yes, you are right. It is O(n). Thank you! Your question makes me think the two
while
loops are redundant. I updated the code (still O(n)
). But, it doesn't change the running time. The test cases maybe not enough.