# 【Leet Code】34. Search for a Ranged---Medium

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,

return [3, 4].

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
if(nums.size() < 1) return {-1, -1};
int index1 = low_boundary(nums, target);
int index2 = low_boundary(nums, target+1) -1;

if(index1 < nums.size() && nums[index1] == target) return {index1, index2};
return {-1,-1};
}

private:
int low_boundary(vector<int>& nums, int target){

int left = 0, right = nums.size() - 1;
int mid = (left + right) / 2;
while(left <= right)
{
mid = (left + right) / 2;
if(target > nums[mid])
left = mid + 1;
else right = mid - 1;
}

return left;
}
};

#### LeetCode 34 Search for a Range (C,C++,Java,Python)

2015-05-14 21:28:11

#### Search for a Range -- LeetCode

2014-03-06 03:48:49

#### LeetCode 34 — Search for a Range（C++ Java Python）

2014-04-03 20:53:45

#### leetcode 34 -- Search for a Range

2015-06-23 00:36:45

#### [LeetCode34]Search for a Range

2014-06-16 01:31:31

#### 【LeetCode-面试算法经典-Java实现】【034-Search for a Range（搜索一个范围）】

2015-07-27 07:17:03

#### leetcode_c++：Search for a Range（034）

2016-05-18 00:45:22

#### Word Search -- LeetCode

2014-04-23 01:21:12

#### 【LeetCode】Word Search II 解题报告

2015-05-19 16:52:33

#### [leet code] Search for a Range

2014-01-23 05:02:03