Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> searchRange(vector<int>& nums, int target) {
- vector<int> result;
- int left_i = searchLeftIndex(nums, target),
- right_i = searchRightIndex(nums, target);
- cout << left_i << endl;
- cout << right_i << endl;
- if (left_i < nums.size() && nums[left_i] == target) {
- result.push_back(left_i);
- result.push_back(right_i);
- } else {
- result.push_back(-1);
- result.push_back(-1);
- }
- return result;
- }
- int searchLeftIndex(vector<int>& nums, int target) {
- int left = 0, right = nums.size();
- while(left < right) {
- int mid = left + (right - left)/2;
- if (nums[mid] >= target) {
- right = mid;
- } else {
- left = mid + 1;
- }
- }
- return left;
- }
- int searchRightIndex(vector<int>& nums, int target) {
- int left = 0, right = nums.size();
- while(left < right) {
- int mid = left + (right - left)/2;
- if (nums[mid] > target) {
- right = mid;
- } else {
- left = mid + 1;
- }
- }
- return right-1;
- }
- };
Add Comment
Please, Sign In to add comment