# Leetcode34. 搜索范围

class Solution(object):
def searchRange(self, nums, target):
left = 0
right = len(nums)-1
while(left<=right):
mid = (left+right)/2
if nums[mid]<target:
left = mid+1
elif nums[mid]>target:
right = mid-1
else:
left_bound = mid
right_bound = mid
while(left_bound-1>=left and nums[left_bound-1]==target):
left_bound -= 1
while(right_bound+1<=right and nums[right_bound+1]==target):
right_bound += 1
return [left_bound,right_bound]
return [-1,-1]

class Solution(object):
def searchRange(self, nums, target):
left = self.searchFirstAppear(nums,target)
right = self.searchEndAppear(nums,target)
return [left,right]

def searchFirstAppear(self,nums,target):
index = -1
left = 0
right = len(nums)-1
while(left<=right):
mid = (left+right)/2
if nums[mid]<target:
left = mid+1
else:
right = mid-1
if nums[mid] == target:
index = mid
return index

def searchEndAppear(self,nums,target):
index = -1
left = 0
right = len(nums)-1
while(left<=right):
mid = (left+right)/2
if nums[mid]<=target:
left = mid+1
else:
right = mid-1
if nums[mid] == target:
index = mid
return index