744. Find Smallest Letter Greater Than Target(大于给定元素的最小元素)(leetcode)

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.

分析:当遇到数组已经排序时的查找题。应先想到二分法。时间复杂度低。

 时间复杂度:o(logn)                         空间复杂度:o(1)

原文地址:https://www.cnblogs.com/shaer/p/10450103.html

时间: 2024-11-09 14:13:06

744. Find Smallest Letter Greater Than Target(大于给定元素的最小元素)(leetcode)的相关文章

744. Find Smallest Letter Greater Than Target 找到大于目标的最小的字母

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

744. Find Smallest Letter Greater Than Target

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

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

目录 题目描述: 示例: 解法: 题目描述: 给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 数组里字母的顺序是循环的.举个例子,如果目标字母target = 'z' 并且有序数组为 letters = ['a', 'b'],则答案返回 'a'. 示例: 输入: letters = ["c", "f", "j"] target = "a" 输出: &quo

[LeetCode] Find Smallest Letter Greater Than Target

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

[Swift]LeetCode744. 寻找比目标字母大的最小字母 | Find Smallest Letter Greater Than Target

Given a list of sorted characters letterscontaining 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 =

二分查找---大于给定元素的最小元素

大于给定元素的最小元素 744. Find Smallest Letter Greater Than Target (Easy) Input: letters = ["c", "f", "j"] target = "d" Output: "f" Input: letters = ["c", "f", "j"] target = "k&qu

Binary search for the first element greater than target

We all know how to search through an array for an element whose value equals the target value, but how to search for the element that has value greater than the target value? A particularly elegant way of thinking about this problem is to think about

17、把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1. NOTE:给出的所有元素都大于0,若数组大小为0,请返回0. eg: 输入 3 4 5 1 2 输出 1 思路:用二分法查找最小元素 三种情况: (1)rotateArray[mid] >rotateArray[high]: like:[x,x,x,6,x,x,2],此时最小数字一

查找出现次数大于n/k的重复元素

本文是对一篇英文论文的总结:Finding Repeated Elements.想看原文,请Google之. 这个问题的简单形式是“查找出现次数大于n/2的重复元素”.我们先从简单问题开始,然后再做扩展. 1.查找出现次数大于n/2的重复元素 <编程之美>中有同样的一道题<寻找发帖水王>,具体思路是每次删除两个不同的元素,最后剩下的就是要求的元素.这个结论的证明如下: 已知:n,m是正整数,n表示数组的长度,m是出现次数大于n/2的元素的个数,即m>n/2. 需要求证的结论包