[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函数 test.caller 谁在调用test函数
返回调用指定函数的函数. function test() { if (test.caller === null) console.log('test 函数在全局调用'); // 获取调用 test函数 ...
- 如何查看目前正在使用的Windows10是哪个版本?
其实相当的简单: win+R 输入winver,就会出现如下图的信息: 就能看到版本信息了
- [No0000B5]C# 类型基础 值类型和引用类型 及其 对象判等 深入研究1
引言 本文之初的目的是讲述设计模式中的 Prototype(原型)模式,但是如果想较清楚地弄明白这个模式,需要了解对象克隆(Object Clone),Clone其实也就是对象复制.复制又分为了浅度复 ...
- Python:正则表达式
学习内容参考:Python正则表达式指南 匹配流程 语法表
- 2014年蓝桥杯省赛A组c++第3题(数组构造+暴力求解)
/* 标题:神奇算式 由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成. 比如: 210 x 6 = 1260 8 x 473 = 3784 27 x 81 = 2187 都符合要 ...
- js Dom对象的属性与方法
1.对象集合: (1).all[]; (2).images[]; (3).anchors[]; (4).forms[]; (5).links[]; ...
- selenium如何定位同级节点
场景:当定位某个元素时,发现所需要的元素在同级节点,可以用/following-sibling::* 方法(定位同级的第二位) 当定位统计节点的第二个定位相邻节点. 可以用/preceding ...
- java System类的一些静态方法
package cn.sasa.demo2; public class SystemDemo { public static void main(String[] args) { func_array ...
- membership DB生成 & dll 强命名 & 证书生成
UPD(Membership)数据库安装1.使用 Aspnet_regsql.exe 安装数据库 在 C:\WINDOWS\Microsoft.NET\Framework\\aspnet_regsql ...
- python numpy初始化一个图像矩阵
mask_all = np.zeros((256, 256), dtype='uint8') 单通道 mask_all_enlarge = np.zeros((256, 256, 3), dtype ...