744 Find Smallest Letter Greater Than Target
Last updated
Was this helpful?
Last updated
Was this helpful?
Given a list of sorted charactersletters
containing only lowercase letters, and given a target lettertarget
, find the smallest element in the list that is larger than the given target.
Letters also wrap around. For example, if the target istarget = 'z'
andletters = ['a', 'b']
, the answer is'a'
.
Examples:
Note:
letters
has a length in range[2, 10000]
.
letters
consists of lowercase letters, and contains at least 2 unique letters.
target
is a lowercase letter.
(1) Binary Search
思路: 非常典型的二分查找 找左边界,思路和search insert position是一样的,其中注意题目要求letters是可以wrap around的 (也就是如果target在数组的插入点是在数组末端的时候,则数组的第一个字母为所求解)
Binary Search: 时间复杂度O(logn), 空间复杂度O(1)