Python 解LeetCode:744. Find Smallest Letter Greater Than Target

思路:二分法,时间复杂度o(logn)

class Solution(object):
    def nextGreatestLetter(self, letters, target):
        """
        :type letters: List[str]
        :type target: str
        :rtype: str
        """
        left, right = 0, len(letters) - 1
        while left <= right:
            mid = (left + right) / 2
            if letters[mid] <= target:
                left = mid + 1
            else:
                if mid < 1 or (mid >= 1 and letters[mid-1] <= target):
                    return letters[mid]
                right = mid - 1
        return letters[0]
posted @ 2017-12-26 22:39  潇湘旧友  阅读(516)  评论(0编辑  收藏  举报