class Solution { public: int loweridx(vector<int> nums ,int target) { int st=0, end=nums.size()-1; int ans=-1; int mid=st+(end-st)/2; while(st<=end) { if(nums[mid]==target) { ans=mid; end=mid-1; } else if(target>nums[mid]) { st=mid+1; } else if(target<nums[mid]) { end=mid-1; } mid=st+(end-st)/2; } return ans; } int largidx(vector<int> nums ,int target) { int st=0, end=nums.size()-1; int ans=-1; int mid=st+(end-st)/2; while(st<=end) { if(nums[mid]==target) { ans=mid; st=mid+1; } else if(target>nums[mid]) { st=mid+1; } else if(target<nums[mid]) { end=mid-1; } mid=st+(end-st)/2; } return ans; } vector<int> searchRange(vector<int>& nums, int target) { vector<int> v; int lowidx=loweridx(nums, target); int laridx=largidx(nums, target); v.push_back(lowidx); v.push_back(laridx); return v; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter