力扣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 ...
随机推荐
- day02-2-商铺查询缓存
功能02-商铺查询缓存 3.商铺详情缓存查询 3.1什么是缓存? 缓存就是数据交换的缓冲区(称作Cache),是存储数据的临时地方,一般读写性能较高. 缓存的作用: 降低后端负载 提高读写效率,降低响 ...
- 32-webpack详细配置-entry
const HtmlWebpackPlugin = require('html-webpack-plugin') const {resolve} = require('path') /** * ent ...
- App复杂动画实现——Rive保姆级教程
作者:京东物流 沈明亮 在App开发过程中,如果想实现动画效果,可以粗略分为两种方式.一种是直接用代码编写,像平移.旋转等简单的动画效果,都可以这么干,如果稍微复杂点,就会对开发工程师的数学功底.图形 ...
- Claude:除ChatGPT外的另一种选择
前言 Claude 是 Anthropic 开发的人工智能产品.Anthropic 是由 11 名前 OpenAI 员工于 2022 年创立的人工智能公司,旨在构建安全.可解释和有益于人类的人工智能系 ...
- 深度学习-08(PaddlePaddle文本分类)
深度学习-08(PaddlePaddle文本分类) 文章目录 深度学习-08(PaddlePaddle文本分类) NLP概述 NLP基本概念 什么是NLP NLP的主要任务 传统NLP方法 传统NLP ...
- AGC061 F Perfect String
毒瘤出题人,史诗加强 AGC 的 F-- 然而我连原题都不会,所以只学了原题做法. 翻译一下题意就是给定一张循环网格图,求经过 \((0,0)\) 的闭合回路条数. 由于网格图中每一个位置都等价,所以 ...
- 如何理解 Spring Boot 中的 Starter ?
假如 没有 Spring Boot Starter,我们有两种方式来创建 Spring Bean. spring xml 模式 (远古模式,并不推荐) spring API 来创建 Spring Be ...
- 🚀 jdbc-plus是一款基于JdbcTemplate增强工具包, 已实现分页、多租户、动态表名等插件,可与mybatis、mybatis-plus等混合使用
jdbc-plus简介 jdbc-plus是一款基于JdbcTemplate增强工具包, 基于JdbcTemplate已实现分页.多租户.动态表名等插件,可自定义扩展插件,可与mybatis.myba ...
- 2023-03-25:若两个正整数的和为素数,则这两个正整数称之为“素数伴侣“。 给定N(偶数)个正整数中挑选出若干对,组成“素数伴侣“, 例如有4个正整数:2,5,6,13, 如果将5和6分为一组的
2023-03-25:若两个正整数的和为素数,则这两个正整数称之为"素数伴侣". 给定N(偶数)个正整数中挑选出若干对,组成"素数伴侣", 例如有4个正整数:2 ...
- 2020-11-26:go中,map的创建流程是什么?
福哥答案2020-11-26: [答案来自此链接:](https://www.bilibili.com/video/BV1Nr4y1w7aa?p=10)源码位于runtime/map.go文件中的ma ...