[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 = ‘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.

找出比目标值大的最小的那个字母

给定一个字符数组,找出比目标值大的最小的那个字符,如果目标值大于数组中最大的字母,则返回数组中第一个字母。

使用upper_bound()这个函数找出比目标字母大的那个数,这时需要判断

如果目标值大于等于数组中最大的字母,则返回数组中的首字母。

否则返回找到的那个字母。

class Solution {
public:
    char nextGreatestLetter(vector<char>& letters, char target) {
        auto it = upper_bound(letters.begin(), letters.end(), target);
        if (it == letters.end())
            return letters.front();
        else {
            return *it;
        }
    }
};
// 12 ms

也可以使用二分搜索。

class Solution {
public:
    char nextGreatestLetter(vector<char>& letters, char target) {
        if (letters.back() <= target)
            return letters.front();
        int left = 0, right = letters.size() - 1;
        while (left < right)  {
            int mid = left + (right - left) / 2;
            if (letters[mid] > target) {
                right = mid;
            }
            else {
                left = mid + 1;
            }
        }
        return letters[left];
    }
};
时间: 2024-11-09 14:13:02

[LeetCode] Find Smallest Letter Greater Than Target的相关文章

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

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

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

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

[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 =

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

[LeetCode] 632. Smallest Range Covering Elements from K Lists

[LeetCode]632. Smallest Range Covering Elements from K Lists 你有 k 个升序排列的整数数组.找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中. 我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b] 比 [c,d] 小. 示例 1: 输入:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] 输出: [20,24] 解释: 列表 1:

Leetcode: K-th Smallest in Lexicographical Order

Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. Note: 1 ≤ k ≤ n ≤ 109. Example: Input: n: 13 k: 2 Output: 10 Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so

LeetCode &quot;483. Smallest Good Base&quot; !!

A more programming-like solution, is to hack the problem from simple: we try each possble base value, and see which 111..11 fits target number - using binary search. class Solution(object): def helper(self, num, p): l = 1; r = int(pow(num, 1.0/p) + 1