在排序数组中查找元素的第一个和最后一个位置(C++实现)

class Solution {
public:
vector<int>v1 = {-1,-1};
vector<int> searchRange(vector<int>& nums, int target) {

if(nums.size()==0)
{
return v1;
}

v1[0] = findfirstPosition(nums,target);

if(v1[0]==-1)
{
return v1;
}
v1[1] = findlastPosition(nums,target);
return v1;
}

private:
int findfirstPosition(vector<int>nums,int target)
{

int left = 0 ;
int right = nums.size()-1;
while(left<=right)
{
int mid = left + (right-left)/2;

if(nums[mid]==target)
{
right = mid - 1;
}

else if(nums[mid]<target)
{
left = mid + 1;
}

else{
right = mid - 1;
}
}

if(left!=nums.size()&&nums[left]==target)
{
return left;
}
return -1;
}

int findlastPosition(vector<int>nums,int target)
{
int left = 0 ;
int right = nums.size()-1;
while(left<=right)
{
int mid = left + (right-left)/2;

if(nums[mid]==target)
{
left = mid + 1;
}

else if(nums[mid]<target)
{
left = mid + 1;
}

else{
right = mid - 1 ;
}
}

return right;
}
};

 

posted @ 2020-11-03 17:55  诗和远方*  阅读(659)  评论(0编辑  收藏  举报