题目见文末

LeetCode link

思路及题解

手写二分

源码:

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

解析

刚开始想到的就是二分查找,在查找之前,先排除两种情况:

  1. 列表首位元素满足要求,返回该元素,程序结束。
  2. 列表末位元素也不满足要求,返回首位元素,程序结束。

    然后开始二分查找。

使用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 orderthe 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' and letters == ['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 <= 104
  • letters[i] is a lowercase English letter.
  • letters is sorted in non-decreasing order.
  • letters contains at least two different characters.
  • target is 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 <= 104
  • letters[i] 是一个小写字母
  • letters 按非递减顺序排序
  • letters 最少包含两个不同的字母
  • target 是一个小写字母

力扣744:寻找比目标字母大的最小字母; LeetCode744:Find Smallest Letter Greater Than Target的更多相关文章

  1. Leetcode744.Find Smallest Letter Greater Than Target寻找比目标字母大的最小字母

    给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 数组里字母的顺序是循环的.举个例子,如果目标字母target = 'z' 并且有 ...

  2. C#版(击败100.00%的提交) - Leetcode 744. 寻找比目标字母大的最小字母 - 题解

    C#版 - Leetcode 744. 寻找比目标字母大的最小字母 - 题解 744.Find Smallest Letter Greater Than Target 在线提交: https://le ...

  3. Leetcode之二分法专题-744. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)

    Leetcode之二分法专题-744. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target) 给定一个只包含小写字母的有序数组letters  ...

  4. Java实现 LeetCode 744 寻找比目标字母大的最小字母(二分法)

    744. 寻找比目标字母大的最小字母 给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 在比较时,数组里字母的是循环有序的.举个例 ...

  5. [Swift]LeetCode744. 寻找比目标字母大的最小字母 | Find Smallest Letter Greater Than Target

    Given a list of sorted characters letterscontaining only lowercase letters, and given a target lette ...

  6. C#LeetCode刷题之#744-寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4001 访问. 给定一个只包含小写字母的有序数组letters 和 ...

  7. 【Leetcode_easy】744. Find Smallest Letter Greater Than Target

    problem 744. Find Smallest Letter Greater Than Target 题意:一堆有序的字母,然后又给了一个target字母,让求字母数组中第一个大于target的 ...

  8. LeetCode 744. Find Smallest Letter Greater Than Target (寻找比目标字母大的最小字母)

    题目标签:Binary Search 题目给了我们一组字母,让我们找出比 target 大的最小的那个字母. 利用 binary search,如果mid 比 target 小,或者等于,那么移到右半 ...

  9. 744. 寻找比目标字母大的最小字母--LeetCode

    来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/find-smallest-letter-greater-than-target 著作权归领扣网络所有. ...

  10. 744. Find Smallest Letter Greater Than Target 查找比目标字母大的最小字母

    [抄题]: Given a list of sorted characters letters containing only lowercase letters, and given a targe ...

随机推荐

  1. STM32新建模板【HAL库】

    看到这篇笔记的小伙伴可能会觉得我在做无用功,明明可以通过 STM32CubeMx 软件直接生成的,还在这里慢慢的创建项目.我觉得在学习的时候最好少借助工具,当我们过度依赖工具的时候,决绝问题的能力可能 ...

  2. resnet18训练自定义数据集

    目录结构 dogsData.py import json import torch import os, glob import random, csv from PIL import Image f ...

  3. python 类中的属性排序

    可以使用Python中的类(class)来定义一个包含姓名和年龄的类.以下是一个示例代码: class Person: def __init__(self, name, age): self.name ...

  4. Object-Advanced笔记

    JavaScript对象进阶 数据劫持 1. 概念 一旦访问或者修改对象的属性时 拦截这个行为(访问/修改) 并对其添加除这个行为外的操作 最后返回结果 2. 实现 2.1 getter和setter ...

  5. ERROR: libfdk_aac not found和ERROR: libmp3lame &gt;= 3.98.3 not dound

    ERROR: libfdk_aac not found和ERROR: libmp3lame >= 3.98.3 not dound 编译ffmepg时出现这两个错误或者有时候需要x264编码的时 ...

  6. Vue中实现数据列表无缝轮播

    类似这种滚动轮播效果 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta char ...

  7. [数据库]MYSQL之授予/查验binlog权限

    在后端做主从备份:亦或是在大数据领域中,各类CDC同步(Canal / Flink CDC等),均会基于MYSQL的binlog来实现. 因此,知道需要哪些权限?怎么查验去.怎么授权就很重要了. 感觉 ...

  8. Prism Sample 9 ChangeConvention

    上个例子跳过了ViewModelLocator,因是采用约定的方式最为方便. 如果有人要修改约定,自定义view和viewModel的默认自动定位方式,怎么办呢? 在app.xaml.cs重写以下方法 ...

  9. Python数据分析中 melt()函数的一些用法

    melt()函数是一个数据重塑工具,用于将宽格式数据转换为长格式数据(Unpivot a DataFrame from wide to long format, optionally leaving ...

  10. ChatGPT 中文指令指南,教会你如何使用chatgpt实现中文你想要的答案

    ChatGPT 中文指令指南,教会你如何使用chatgpt实现中文你想要的答案 1.学习英语--替代词典 App 场景 例子 Prompts 解释中文英文意思,并解释单词的词根词缀.可以替代词典. 告 ...