Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- knakul853
- **/
- class Solution {
- public:
- vector<int> searchRange(vector<int>& nums, int target) {
- vector<int>ans;
- ans.push_back(lower_bound(nums, target));
- ans.push_back(upper_bound(nums, target));
- return ans;
- }
- int lower_bound(vector<int>& nums, int t)
- {
- int l=0, r=(int)nums.size()-1;
- int ans = -1;
- while(l<=r)
- {
- int mid = l + ( r - l )/2;
- if(nums[mid] == t)
- {
- ans = mid;
- }
- if(nums[mid] >= t){
- r = mid - 1;
- }
- else l = mid + 1;
- }
- return ans;
- }
- int upper_bound(vector<int>& nums, int t)
- {
- int l=0, r=(int)nums.size()-1;
- int ans = -1;
- while(l<=r)
- {
- int mid = l + ( r - l )/2;
- if(nums[mid] == t)
- {
- ans = mid;
- }
- if(nums[mid] > t){
- r = mid - 1;
- }
- else l = mid + 1;
- }
- return ans;
- }
- };
Add Comment
Please, Sign In to add comment