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.

思路存ans和看到的最小的char, 如果没有ans, 我们就返回最小的char. O(n)

Code

class Solution:
def nextGreatestLetter(self, letters, target):
ans = [None] *2 # [0] 记录ans, [1] 记录最小的letters
for c in letters:
if not ans[1] or ord(c) < ord(ans[1]):
ans[1] = c
if ord(c) > ord(target) and (not ans[0] or ord(ans[0]) > ord(c)):
ans[0] = c
return ans[0] if ans[0] else ans[1]

[LeetCode] 744. Find Smallest Letter Greater Than Target_Easy tag: **Binary Search的更多相关文章

  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】744. Find Smallest Letter Greater Than Target 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 二分查找 日期 题目地址:https:// ...

  5. [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 ...

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

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

  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] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

随机推荐

  1. CentOS7.5 Linux搭建全文检索--Solr7.4.0单机服务

    一.Solr安装环境 1.官方参考文档 Solr教程参考指南:http://lucene.apache.org/solr/guide/7_4/solr-tutorial.html 2.Solr运行环境 ...

  2. UDP协议的例子

    public class Service { // 服务器         public static void main(String[] args) {               Datagra ...

  3. Spark RDD Action 简单用例(二)

    foreach(f: T => Unit) 对RDD的所有元素应用f函数进行处理,f无返回值./** * Applies a function f to all elements of this ...

  4. datagrid--新增

    先在datagrid中添加toolbar配置项,增删改差的按钮,有3个属性,按钮名称,图标,回调函数,点击按钮会弹出一个对话框dialog,dialog是关闭的,closed=true, toolba ...

  5. mtd工具

    http://daemons.net/linux/storage/mtd.html MTD The Memory Technology Devices (MTD) subsystem provides ...

  6. 品尝阿里云容器服务:初步尝试ASP.NET Core Web API站点的Docker自动化部署

    部署场景是这样的,我们基于 ASP.NET Core 2.0 Preview 1 开发了一个用于管理缓存的 Web API ,想通过阿里云容器服务基于 Docker 部署为内网服务. 在这篇博文中分享 ...

  7. ROS中.launch文件的remap标签详解

    https://www.cnblogs.com/LiuQiang921202/p/7679943.html

  8. vins-mono调试教程

    https://www.cnblogs.com/Jessica-jie/p/6950604.html

  9. 一些shell默认的变量

    测试脚本如下,我这里主要想测试$0,$1,$2,$n,$@,$*默认都代表了什么? #!/bin/sh echo '$1='$1 echo '$2='$2 echo '$@='$@ echo '$*= ...

  10. 可变数组(PLSQL)

    可变数组 可变数组与嵌套表相似,也是一种集合.一个可变数组是对象的一个集合,其中每个对象都具有相同的数据类型.可变数组的大小由创建时决定.在表中建立可变数组后,可变数组在主表中作为一个列对待.从概念上 ...