Search for all the exist numbers and sort (C++)

• Here is my code for this question:

Firstly, find all the exist target in the num vector using binary search and put the index in a vector named 'res'.

Secondly, sort 'res' and output the first and last element in 'res' as the final result;

``````void bs(vector<int> &nums,int start,int end, int target, vector<int> &res){
if(start>end) return;
if(start == end){
if(nums[start] == target)
res.push_back(start);
return;
}
int mid = (start+end)/2;
if(nums[mid] == target){
res.push_back(mid);
bs(nums,start,mid-1,target,res);
bs(nums,mid+1,end,target,res);
}
else{
if(nums[mid] < target)
bs(nums,mid+1,end,target,res);
else
bs(nums,start,mid-1,target,res);
}
}
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> res;
bs(nums,0,nums.size()-1,target,res);
vector<int> r;
if(res.empty()){
r.push_back(-1);
r.push_back(-1);
return r;
}
sort(res.begin(),res.end());
r.push_back(res[0]);
r.push_back(res[res.size()-1]);
return r;
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.