原题地址(简单)
方法1—二分查找
经典的二分查找题目
代码
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
n = len(letters)
low, high = 0, n - 1
while low <= high:
mid = low + (high - low) // 2
if letters[mid] > target:
high = mid - 1
else: # letters[mid] < target
low = mid + 1
return letters[low % n]
时空复杂度
时间复杂度 O(logN)
空间复杂度 O(1)