关闭

Search for a Range

标签: leetcode
130人阅读 评论(0) 收藏 举报
分类:
class Solution {
public:
	vector<int> searchRange(vector<int>& nums, int target) {
		vector<int> result;
		int tmp = 0;
		int left = 0, right = nums.size(), mid = 0;
		//if (left == right) return nums[0] == target ? vector<int>{0, 0} : vector<int>{ -1, -1 };
		while (left<right){
			mid = (left + right) / 2;
			if (nums[mid] == target){
				tmp = mid;
				while (mid>0&&nums[mid - 1] == target) mid--;
				while (tmp<nums.size()-1&&nums[tmp + 1] == target) tmp++;
				return vector<int> {mid, tmp};
			}
			else if (nums[mid]<target){
				left = mid + 1;
			}
			else{
				right = mid;
			}

		}
		return vector<int> {-1, -1};
	}
};

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:47848次
    • 积分:1061
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:33篇
    • 译文:0篇
    • 评论:8条
    最新评论