赞
踩
题目:
题解:
- class Solution {
- public:
- int binarySearch(vector<int>& nums, int target, bool lower) {
- int left = 0, right = (int)nums.size() - 1, ans = (int)nums.size();
- while (left <= right) {
- int mid = (left + right) / 2;
- if (nums[mid] > target || (lower && nums[mid] >= target)) {
- right = mid - 1;
- ans = mid;
- } else {
- left = mid + 1;
- }
- }
- return ans;
- }
-
- vector<int> searchRange(vector<int>& nums, int target) {
- int leftIdx = binarySearch(nums, target, true);
- int rightIdx = binarySearch(nums, target, false) - 1;
- if (leftIdx <= rightIdx && rightIdx < nums.size() && nums[leftIdx] == target && nums[rightIdx] == target) {
- return vector<int>{leftIdx, rightIdx};
- }
- return vector<int>{-1, -1};
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。