34-Search-for-a-Range https://leetcode.com/problems/search-for-a-range/
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
high = len(nums) - 1
low = 0
return [self.lowBound(low, high, nums, target), self.highBound(low, high, nums, target)]
def lowBound(self, low, high, nums, target):
while low < high:
mid = (low + high) / 2
if nums[mid] < target:
low = mid + 1
elif nums[mid] >target:
high = mid - 1
else:
high = mid
return low if nums[low] == target else -1
def highBound(self, low, high, nums, target):
while low < high:
mid = (low + high + 1) / 2
if nums[mid] > target:
high = mid - 1
elif nums[mid] < target:
low = mid + 1
else:
low = mid
return high if nums[high] == target else -1
Total Accepted: 127454 Total Submissions: 409695 Difficulty: Medium Contributor: LeetCode Given an array of integers sorted in ascending order, 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]
.