作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/

题目描述

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:

  1. letters has a length in range [2, 10000].
  2. letters consists of lowercase letters, and contains at least 2 unique letters.
  3. target is a lowercase letter.

题目大意

给出了一个排序的字符数组,找出第一个比target大的字符。注意是数组是循环的。

解题方法

线性扫描

找到第一个比指定字符大的。如果没找到的话,列表是可以循环的。也就是说如果没找到就返回列表第一个字符。

class Solution(object):
def nextGreatestLetter(self, letters, target):
"""
:type letters: List[str]
:type target: str
:rtype: str
"""
for letter in letters:
## 提交了之后发现不用使用ord,字符可以用'>''<'比较大小
if ord(letter) > ord(target):
return letter
return letters[0]

二分查找

因为是有序的,所以直接二分即可。

class Solution(object):
def nextGreatestLetter(self, letters, target):
"""
:type letters: List[str]
:type target: str
:rtype: str
"""
index = bisect.bisect_right(letters, target)
return letters[index % len(letters)]

日期

2018 年 1 月 23 日
2018 年 11 月 19 日 —— 周一又开始了

【LeetCode】744. Find Smallest Letter Greater Than Target 解题报告(Python)的更多相关文章

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

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

  2. LeetCode 744. Find Smallest Letter Greater Than Target (时间复杂度O(n))

    题目 太简单了,直接上代码: class Solution { public: char nextGreatestLetter(vector<char>& letters, cha ...

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

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

  4. [LeetCode&Python] Problem 744. Find Smallest Letter Greater Than Target

    Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...

  5. Python 解LeetCode:744. Find Smallest Letter Greater Than Target

    思路:二分法,时间复杂度o(logn) class Solution(object): def nextGreatestLetter(self, letters, target): "&qu ...

  6. [LeetCode] 744. Find Smallest Letter Greater Than Target_Easy tag: **Binary Search

    Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...

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

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

  8. 744. Find Smallest Letter Greater Than Target

    俩方法都是用二分查找,一个调库,一个自己写而已. 方法一,调库 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NUL ...

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

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

随机推荐

  1. 暂时lvs

    负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F5.Netscale.这里主要是学 ...

  2. javaSE高级篇2 — 流技术 — 更新完毕

    1.先认识一个类----File类 前言:IO相关的一些常识 I / O----输入输出 I     输入     input 0    输出     output I / o 按数据的流动方向来分- ...

  3. java输入代码

    import java.util.Scanner; public class Demo59 {    public static void main(String[] args) {        / ...

  4. 巩固javaweb的第二十三天

    巩固内容: 调用验证方法 验证通常在表单提交之前进行,可以通过按钮的 onClick 事件,也可以通过 form 表单 的 onSubmit 事件来完成. 本章实例是通过 form 表单的 onSub ...

  5. 关于vue-cli中-webkit-flex-direction: column失效问题

    我最近在用vue-cli更新项目,在我引入layer.css后会报错并且使用弹性盒时查看元素的时候没有-webkit-flex-direction: column这个属性会失效 这个本身就不打算给di ...

  6. Gradle入门及SpringBoot项目构建

    https://blog.csdn.net/qq_27520051/article/details/90384483 一.介绍 Gradle 是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是 ...

  7. MyBatis中sql实现时间查询的方法

    <if test="startTime != null and startTime !=''"> AND lTime >= #{startTime} </i ...

  8. html href页面跳转获取参数

    //传递参数 var id = columnData.id; var companyname = encodeURI(columnData.companyname); var linename = e ...

  9. SpringMVC(2):JSON

    一,JSON 介绍 JSON (JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效 ...

  10. my38_MySQL事务知识点零记

    从innodb中查看事务信息 show engine innodb status\G; ------------ TRANSACTIONS------------Trx id counter 3153 ...