[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 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:
lettershas a length in range[2, 10000].lettersconsists of lowercase letters, and contains at least 2 unique letters.targetis 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的更多相关文章
- LeetCode 744. Find Smallest Letter Greater Than Target (寻找比目标字母大的最小字母)
题目标签:Binary Search 题目给了我们一组字母,让我们找出比 target 大的最小的那个字母. 利用 binary search,如果mid 比 target 小,或者等于,那么移到右半 ...
- LeetCode 744. Find Smallest Letter Greater Than Target (时间复杂度O(n))
题目 太简单了,直接上代码: class Solution { public: char nextGreatestLetter(vector<char>& letters, cha ...
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 二分查找 日期 题目地址:https:// ...
- [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 ...
- Python 解LeetCode:744. Find Smallest Letter Greater Than Target
思路:二分法,时间复杂度o(logn) class Solution(object): def nextGreatestLetter(self, letters, target): "&qu ...
- 744. Find Smallest Letter Greater Than Target 查找比目标字母大的最小字母
[抄题]: Given a list of sorted characters letters containing only lowercase letters, and given a targe ...
- 744. Find Smallest Letter Greater Than Target
俩方法都是用二分查找,一个调库,一个自己写而已. 方法一,调库 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NUL ...
- [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. ...
随机推荐
- MongoDB数据库基础
MongoDB简介 MongoDB是一种文档型的非关系型数据库(NoSQL),举例如下: {“foo”:,"greeting":"Hello,world!"} ...
- 常见的压缩文件格式案例tarZ
在AIX上最常见的压缩文件就是.tar压缩格式的文件了. 而除了tar文件以外,有时会遇到数据是用其它的压缩文件格式,所以偶顺手整理了一些常见的压缩文件格式,在AIX要怎么解压缩 : 一. .tar ...
- POJ 1655 - Balancing Act - [DFS][树的重心]
链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...
- VS在解决方案中添加一个别人给的项目,我自己的项目主窗体中不能调用
提示缺少Using引用,我在主窗体中已经写了Using XX,还是提示“未能找到类型或命名空间名“ XX”(是否缺少Using指令或程序集引用?)”,以前只要Using 一下就好了,后来想了一下,要在 ...
- PE/ELF文件里面存的是什么呢?
malloc分配的空间是连续的吗?PE/ELF文件里面存的是什么呢?
- 斜率优化&单调性优化的相似性
写了一道单调性优化发现 跟斜率优化很像,而且这道题目感觉质量非常的好. 其实斜率优化是基于单调性优化的,但是面对这道题 我竟然连单调性优化都不太会,尽管这个模型非常不好理解. 对于每道题 我都会打一个 ...
- day2_jmeter断言
断言就是:检查返回结果,测试是否通过,就是自动帮你校验结果用的 1)添加响应断言 我们模式匹配规格选择包括
- 根据Request获取客户端IP
转自: http://www.cnblogs.com/icerainsoft/p/3584532.html http://www.cnblogs.com/bingya/articles/3134227 ...
- Java如何对List集合的操作方法(二)
4.list中查看(判断)元素的索引: 注意:.indexOf(): 和 lastIndexOf()的不同: ///*************************************** ...
- 【PyQt5-Qt Designer】界面布局
PyQt5 界面布局详谈 箱式布局 QHBoxLayout和QVBoxLayout是基本的布局类,它们在水平和垂直方向上排列小部件 效果图: from PyQt5.QtCore import Qt f ...