python写算法题:leetcode: Add to List 34. Search for a Range

来源:互联网 发布:电影推荐2017知乎 编辑:程序博客网 时间:2024/06/01 08:59

https://leetcode.com/problems/search-for-a-range/description/

class Solution(object):    def searchRange(self, nums, target):        """        :type nums: List[int]        :type target: int        :rtype: List[int]        """        start=0        end=len(nums)-1        while start<=end:            half = (start+end)/2            if target<=nums[half]:                end=half-1            else:                start=half+1        if start<len(nums) and nums[start] == target:            pos0=start        elif start+1 < len(nums) and nums[start+1] == target:            pos0=start+1        else:            return [-1, -1]        start=pos0        end=len(nums)-1        while start<=end:            half = (start+end)/2            if target>=nums[half]:                start=half+1            else:                end=half-1        if start < len(nums) and nums[start] == target:            pos1=start        else:            pos1=start-1        return [pos0, pos1]


原创粉丝点击