[抄题]:

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"

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

Input:
letters = ["c", "f", "j"]
target = "k"
Output: "c"
结果是最后一位,但是需要返回第一位 结果%n (看余数 不止看倍数)

[思维问题]:

“第一个最大”居然没看出来是二分查找问题。字母换成index数字后继续操作啊

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

“第一个最大”居然没看出来是二分查找问题。字母换成index数字后继续操作啊

[复杂度]:Time complexity: O(lgn) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

九章的不行就用这一套:

while (lo < hi)

lo = mid + 1
//Terminal condition is 'lo < hi', to avoid infinite loop when target is smaller than the first element
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] > x) hi = mid;
else lo = mid + 1; //a[mid] <= x
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public char nextGreatestLetter(char[] letters, char target) {
//ini
int n = letters.length;
int start = 0, end = n; //bs
while (start < end) {
int mid = start + (end - start) / 2;
if (target < letters[mid]) {
end = mid;
}else {
start = mid + 1;
}
} //return
return letters[start % n];
}
}

744. Find Smallest Letter Greater Than Target 查找比目标字母大的最小字母的更多相关文章

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

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

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

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

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

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

  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. 744. Find Smallest Letter Greater Than Target

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

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

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

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

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

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

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

随机推荐

  1. 使用Audition录制自己的歌曲

    Audition专为在照相室.广播设备和后期制作设备方面工作的音频和视频专业人员设计,可提供先进的音频混合.编辑.控制和效果处理功能.最多混合 128 个声道,可编辑单个音频文件,创建回路并可使用 4 ...

  2. asp.net core microservices 架构之 分布式自动计算(二)

    一  简介                   上一篇介绍了zookeeper如何进行分布式协调,这次主要讲解quartz使用zookeeper进行分布式计算,因为上一篇只是讲解原理,而这次实际使用, ...

  3. P2P技术基础: 关于TCP打洞技术

    4 关于TCP打洞技术 建立穿越NAT设备的p2p的 TCP 连接只比UDP复杂一点点,TCP协议的“打洞”从协议层来看是与UDP的“打洞”过程非常相似的.尽管如此,基于TCP协议的打洞至今为止还没有 ...

  4. Rancher使用入门

    http://tonybai.com/2016/04/14/an-introduction-about-rancher/

  5. 30G 的redis 如何优化

    突然发现我们的redis 已经用了30G了,好吧这是个很尴尬的数字因为我们的缓存机器的内存目前是32G的,内存已经告竭.幸好上上周公司采购了90G的机器,现在已经零时迁移到其中的一台机器上了.(跑题下 ...

  6. 第16篇 Shell脚本基础(一)

    1.什么是shell?shell是一个命令解释器. 是介于操作系统内核与用户之间的一个绝缘层.对于一个linux系统使用人员来说,shell是你驾驭类linux系统最基本的工具.所有的系统命令和工具再 ...

  7. Use the dkms from EPEL when install CUDA Toolkits on CentOS

    ###Use the dkms from EPEL. yum install epel-release yum install dkms # download the rpm from the NVi ...

  8. Volley请求图片

    ImageRequest imageRequest = new ImageRequest(Config.USER_ASSETS_URL + md5(userid) + "/images/av ...

  9. bootstrap 设置表格固定宽度 内容换行

    在项目中开发的时候用的bootstrap,但是有些表格的内容 会显示的很长 那么我第一时间想到的就是 修改td或者th的width,但是我设置了 之后不起作用 于是百度找到了解决方法: 学习源头: h ...

  10. .net中webconfig自定义配置

    在configuration节点,也就是文件的根节点下,增加如下节点 <appSettings> <!--<add key="propPath" value ...