# LeetCode: 34. 搜索范围¶

## 1、题目描述¶

输入: nums = [5,7,7,8,8,10], target = 8



输入: nums = [5,7,7,8,8,10], target = 6



## 2、解题思路¶

​ 首先，我么能使用二分法，找到这个元素在数组中的位置，如果找不到，就返回[-1,-1]

​ 如果找得到，表示继续使用二分法，找出最小，和最大的下标

class Solution:
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
left = 0
right = len(nums) - 1
middle = (left + right) // 2
result = [-1, -1]
while left <= right:
if nums[middle] == target:
result[0] = self.findMinIndex(nums, middle, target)
result[1] = self.findMaxIndex(nums, middle, target)

if nums[middle] < target:
left = middle + 1

else:
right = middle - 1
middle = (left + right) // 2
return result

def findMinIndex(self, nums, current, target):
left = 0
right = current

middle = (left + right) // 2
while left <= right:
if nums[middle] == target and middle == 0:
return middle
if nums[middle] == target and middle > 0 and nums[middle - 1] != target:
return middle

if nums[middle] < target:
left = middle + 1
else:
right = middle - 1
middle = (left + right) // 2
return current

def findMaxIndex(self, nums, current, target):
left = current
right = len(nums) - 1

middle = (left + right) // 2
while left <= right:
if nums[middle] == target and middle == len(nums) - 1:
return middle
if nums[middle] == target and middle > 0 and nums[middle + 1] != target:
return middle

if nums[middle] > target:
right = middle - 1
else:
left = middle + 1
middle = (left + right) // 2
return current