思路:二分法。
class Solution(object):
def nextGreatestLetter(self, letters, target):
letters = list(set(letters))
letters.sort()
if target in letters:
index = letters.index(target)
if index < len(letters) - 1:
return letters[(index % len(letters)) + 1]
if index == len(letters) - 1:
return letters[(index + 1) % len(letters)]
l, r = 0, len(letters) - 1
while l < r:
mid = int(l + (r - l) / 2)
if letters[mid] > target:
r = mid
else:
l = mid + 1
if l == len(letters) - 1 and letters[-1] < target:
return letters[(l + 1) % len(letters)]
else:
return letters[l % len(letters)]