[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:
letters
has a length in range[2, 10000]
.letters
consists of lowercase letters, and contains at least 2 unique letters.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的更多相关文章
- 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. ...
随机推荐
- js中parseInt和Number
昨天在项目中遇到一个问题,有关字符串准换成数字的问题,具体如下: 页面中<input type="number" id="bankNum" ng-mode ...
- int main(int argc,char *argv[])与int main(int argc,char **argv)区别?
int main(int argc,char *argv[])与int main(int argc,char **argv)区别? 这两种是一个等价的写法 而int main(int argc,cha ...
- rtd1296 mtd 设备驱动分析
mtd 分区一般采用3种方式实现 1.内核写死 mtd_partition 2.u-boot 传参 为了使kernel能够解析mtdparts信息,我们需要将内核中的Device Drivers - ...
- winform进度条
参考资料: http://www.cnblogs.com/zzy0471/archive/2010/12/12/1903602.html http://www.cnblogs.com/haogj/ar ...
- sql 一对多查询
1. 一对多查询 查询departmentinfo字典下所有部门的人员数量 select * from departmentinfo a left join (select count(*) User ...
- ssh agent-forward
出于安全性考虑,服务器迁移后,将统一使用 SSH agent forwarding 方式登录所有服务器, 原则上所有 ssh 操作都要通过跳板机,而且跳板机上禁止存储一切私钥. 在此说明一下后续ssh ...
- [skill] mmap / fwrite / write linux磁盘读写的分层结构
转自:http://www.cnblogs.com/zhaoyl/p/5901680.html 看完此文,题目不言自明.转自 http://blog.chinaunix.net/uid-2710571 ...
- 虚拟机窗口太小_设置分辨率(win8/win7)
虚拟机安装了WIN7和WIN8系统后,安装了VMware Tools后窗口还是较小,需要调整虚拟机系统中的分辨率. 桌面右键->屏幕分辨率->设置成与主机显示器分辨率相近的即可.
- 催希凡javaweb 学习28天
看到这样的博客,自己也在看传智播客的视频,收藏一下 催希凡javaweb 学习28天 http://www.cnblogs.com/Prozhu/category/824899.html
- ASCLL码中的一些小知识
其次要记住asill值中 65是A 97是a A与a之间相隔32,用int转换后再用char转换回来. char b = s.charAt(i);为字符串转换成一个一个的.