力扣744:寻找比目标字母大的最小字母; LeetCode744:Find Smallest Letter Greater Than Target
题目见文末
思路及题解
手写二分
源码:
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
if letters[0] > target or letters[-1] <= target:
return letters[0]
start = 1
end = len(letters)-1
while start < end:
mid = (start + end) // 2
if letters[mid] > target:
end = mid
else:
start = mid + 1
return letters[start]
执行结果
40 ms, 16.9 MB
解析
刚开始想到的就是二分查找,在查找之前,先排除两种情况:
- 列表首位元素满足要求,返回该元素,程序结束。
- 列表末位元素也不满足要求,返回首位元素,程序结束。
然后开始二分查找。
使用python内置函数
源码
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
r = bisect_right(letters, target)
if r == len(letters):
r = 0
return letters[r]
执行结果
44 ms, 16.9 MB
解析
python 内置了二分算法操作的模块bisect,如果不想手写的话,也可以使用内置函数。

根据题意:sorted in non-decreasing order,the smallest character in the array that is larger than target ,所以使用 bisect_right 函数确定位置:
def bisect_right(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e <= x, and all e in
a[i:] have e > x. So if x already appears in the list, a.insert(x) will
insert just after the rightmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
# Use __lt__ to match the logic in list.sort() and in heapq
if x < a[mid]: hi = mid
else: lo = mid+1
return lo
题目
英文版
744. Find Smallest Letter Greater Than Target
Given a characters array letters that is sorted in non-decreasing order and a character target, return the smallest character in the array that is larger than target.
Note that the letters wrap around.
- For example, if
target == 'z'andletters == ['a', 'b'], the answer is'a'.
Example 1:
Input: letters = ["c","f","j"], target = "a"
Output: "c"
Example 2:
Input: letters = ["c","f","j"], target = "c"
Output: "f"
Example 3:
Input: letters = ["c","f","j"], target = "d"
Output: "f"
Constraints:
2 <= letters.length <= 104letters[i]is a lowercase English letter.lettersis sorted in non-decreasing order.letterscontains at least two different characters.targetis a lowercase English letter.
中文版
744. 寻找比目标字母大的最小字母
给你一个排序后的字符列表 letters ,列表中只包含小写英文字母。另给出一个目标字母 target,请你寻找在这一有序列表里比目标字母大的最小字母。
在比较时,字母是依序循环出现的。举个例子:
- 如果目标字母
target = 'z'并且字符列表为letters = ['a', 'b'],则答案返回'a'
示例 1:
输入: letters = ["c", "f", "j"],target = "a"
输出: "c"
示例 2:
输入: letters = ["c","f","j"], target = "c"
输出: "f"
示例 3:
输入: letters = ["c","f","j"], target = "d"
输出: "f"
提示:
2 <= letters.length <= 104letters[i]是一个小写字母letters按非递减顺序排序letters最少包含两个不同的字母target是一个小写字母
力扣744:寻找比目标字母大的最小字母; LeetCode744:Find Smallest Letter Greater Than Target的更多相关文章
- Leetcode744.Find Smallest Letter Greater Than Target寻找比目标字母大的最小字母
给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 数组里字母的顺序是循环的.举个例子,如果目标字母target = 'z' 并且有 ...
- C#版(击败100.00%的提交) - Leetcode 744. 寻找比目标字母大的最小字母 - 题解
C#版 - Leetcode 744. 寻找比目标字母大的最小字母 - 题解 744.Find Smallest Letter Greater Than Target 在线提交: https://le ...
- Leetcode之二分法专题-744. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)
Leetcode之二分法专题-744. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target) 给定一个只包含小写字母的有序数组letters ...
- Java实现 LeetCode 744 寻找比目标字母大的最小字母(二分法)
744. 寻找比目标字母大的最小字母 给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 在比较时,数组里字母的是循环有序的.举个例 ...
- [Swift]LeetCode744. 寻找比目标字母大的最小字母 | Find Smallest Letter Greater Than Target
Given a list of sorted characters letterscontaining only lowercase letters, and given a target lette ...
- C#LeetCode刷题之#744-寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4001 访问. 给定一个只包含小写字母的有序数组letters 和 ...
- 【Leetcode_easy】744. Find Smallest Letter Greater Than Target
problem 744. Find Smallest Letter Greater Than Target 题意:一堆有序的字母,然后又给了一个target字母,让求字母数组中第一个大于target的 ...
- LeetCode 744. Find Smallest Letter Greater Than Target (寻找比目标字母大的最小字母)
题目标签:Binary Search 题目给了我们一组字母,让我们找出比 target 大的最小的那个字母. 利用 binary search,如果mid 比 target 小,或者等于,那么移到右半 ...
- 744. 寻找比目标字母大的最小字母--LeetCode
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/find-smallest-letter-greater-than-target 著作权归领扣网络所有. ...
- 744. Find Smallest Letter Greater Than Target 查找比目标字母大的最小字母
[抄题]: Given a list of sorted characters letters containing only lowercase letters, and given a targe ...
随机推荐
- android 6.0动态权限的申请
一下9组权限需要动态申请//通讯录(读.写.查询)group:android.permission-group.CONTACTS permission:android.permission.WRITE ...
- 2021年蓝桥杯python组真题-直线(细节+分析+代码)
题目 : 代码: #直线 M=[[x,y] for x in range(20) for y in range(21)] #创建二维列表:代表xy坐标系 d=set() #创建集合属性的容器:因为集合 ...
- xlsx纯前端导出表格,完善边框等样式
仅用xlsx是无法实现文字样式及表格边框的style的,因此配合使用xlsx-style 以下源码直接复制过去用 // 源码什么的都不需要改动 import * as XLSXStyle from ' ...
- docker常用命令之帮助启动类命令和镜像命令
docker安装之后,启动时会报如下错误: Job for docker.service failed because start of the service was attempted too o ...
- 文盘Rust —— rust连接oss | 京东云技术团队
作者:京东科技 贾世闻 对象存储是云的基础组件之一,各大云厂商都有相关产品.这里跟大家介绍一下rust与对象存储交到的基本套路和其中的一些技巧. 基本连接 我们以 [S3 sdk]( https:// ...
- Vue 前端开发团队风格指南(史上最全)
Vue官网的风格指南按照优先级(依次为必要.强烈推荐.推荐.谨慎使用)分类,本文根据项目实际情况整理了一份适用于团队开发的vue风格指南,供大家参考. 一.命名规范 常用的命名规范: camelCas ...
- #PowerBi 1分钟学会,在excel中,调用powerbi数据模型(Analyze in Excel插件)
在工作中,我们常常使用excel来进行临时的数据处理服务,如果我们在powerbi中,已经有了完整的数据模型. 那么我们都可以通过直接调用powerbi数据模型,来进行快速的数据分析,完成任务. 今天 ...
- Python NumPy 广播(Broadcast)
广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 如果两个数组 a 和 b 形状相同,即满足 a.shape == ...
- 18年CCCC赛后总结
C4赛后总结: 我正式入坑以来,大约5个月,这也是我第一次出去参与这样正式的比赛,其实比赛结果并不尽人意,但有很多还是需要记录下来的,通过这次比赛的确获得了很多的比赛经验: 一赛前: 其实赛前的状态, ...
- golang技术栈常见网址
go所有,包含goadmin golang标准库文档 golang修养之路 Golang Profiling: 关于 pprof go问题 go语言设计与实现 go.mod解析 proto3 prot ...