leetcode 744. Find Smallest Letter Greater Than Target
Given a list of sorted characters letters
containing only lowercase letters, and given a target letter target
, find the smallest element in the list that is larger than the given target.
Letters also wrap around. For example, if the target is target = 'z'
and letters = ['a', 'b']
, the answer is 'a'
.
Examples:
Input: letters = ["c", "f", "j"] target = "a" Output: "c" Input: letters = ["c", "f", "j"] target = "c" Output: "f" Input: letters = ["c", "f", "j"] target = "d" Output: "f" Input: letters = ["c", "f", "j"] target = "g" Output: "j" Input: letters = ["c", "f", "j"] target = "j" Output: "c" Input: letters = ["c", "f", "j"] target = "k" Output: "c"
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.class Solution(object): def nextGreatestLetter(self, letters, target): """ :type letters: List[str] :type target: str :rtype: str """ ''' Input: letters = ["c", "f", "j"] target = "j" Output: "c" Input: letters = ["c", "f", "j"] target = "k" Output: "c" just check the last letter if letters[-1] <= target: return letters[0] Input: letters = ["c", "f", "j"] target = "a" Output: "c" just check the first letter .... Input: letters = ["c", "f", "j"] target = "c" Output: "f" use binsearch mid letter > target and mid-letter <= target Input: letters = ["c", "f", "j"] target = "d" Output: "f" Input: letters = ["c", "f", "j"] target = "g" Output: "j" ''' if letters[0] > target or letters[-1] <= target: return letters[0] i = 0 j = len(letters)-1 while i <= j: mid = (i+j) >> 1 if letters[mid] <= target: i = mid + 1 else: if letters[mid-1] <= target: return letters[mid] else: j = mid -1
就是二分变种,没啥说的,但是要防止mid-1 < 0,因此,先判断了一下,确保target在letters里面。
-
123456789101112131415161718
class
Solution(
object
):
def
nextGreatestLetter(
self
, letters, target):
"""
:type letters: List[str]
:type target: str
:rtype: str
"""
length
=
len
(letters)
if
letters[
0
] > target
or
letters[
-
1
] <
=
target:
return
letters[
0
]
i, j
=
0
, length
-
1
while
i <
=
j:
mid
=
(i
+
j) >>
1
if
letters[mid] <
=
target:
i
=
mid
+
1
else
:
j
=
mid
-
1
return
letters[i]
本质,循环结束一定有,i=j+1, 在循环体里结束前一刻必然有,j==i==mid,走到判断那里,要么j=mid-1(满足target <letters[mid]),或者i=mid+1(letters[mid] <= target),所以一定有letters[j] <= target < letters[i]
-
补充:bisect_right,
- #在L中查找x,x存在时返回x右侧的位置,
- x不存在返回应该插入的位置..
- >>> import bisect
>>> L = [1,3,3,6,8,12,15]
>>> bisect.bisect_right(L,3)
3
>>> bisect.bisect_right(L,7)
4
>>> bisect.bisect_right(L,15)
7
>>> bisect.bisect_right(L,19)
7
>>> bisect.bisect_right(L,1)
1
>>> bisect.bisect_right(L,0)
0
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2017-04-06 bleve搜索引擎源码分析之索引——mapping真复杂啊
2017-04-06 我的vim 配置——nerdtree、ack vim、vim sneak