给出一列一排序的数组,问某个数target在里面第一次和最后一次出现的位置,二分,用python的bisect很方便
#34
#Runtime: 67 ms (Beats 6.68%)
#Memory: 14.4 MB (Beats 65.32%)
class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
l = bisect.bisect_left(nums, target)
r = bisect.bisect_right(nums, target)
if l > r - 1:
return [-1, -1]
return [l, r - 1]