leetcode 34在排序数组中查找元素的第一个和最后一个位置
class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { vector<int> res(2,-1); int left=0;int right=nums.size()-1; if(right<0) return res; if(right==0){ if(nums[0]==target) return{0,0}; else return res; } int mid; //寻找lower_bound while(left<right){ int mid=left+(right-left)/2; if(nums[mid]<target) left=mid+1; else right=mid; } if(nums[left]==target){ res[0]=left; int step=1; while(step+left<nums.size()&&nums[step+left]==target) step++; res[1]=left+step-1; } return res; } };