34. Search for a Range
本文共 1095 字,大约阅读时间需要 3 分钟。
class Solution {private: int binerySearch(vector &nums,int searchnum,int start,int end) { if(start<=end) { int mid=(start+end)/2; if(nums[mid]==searchnum) return mid; else if(searchnum
result; if(nums.size()==0) { result.push_back(-1); result.push_back(-1); return result; } int findfirst=binerySearch(nums,target,0,nums.size()-1); if(findfirst==-1) { result.push_back(-1); result.push_back(-1); }else { int left=findfirst; int right=findfirst; while(left>=0&&nums[left]==target) { left--; } left++; while(right<=nums.size()-1&&nums[right]==target) { right++; } right--; result.push_back(left); result.push_back(right); } return result; }};