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

目录

  • 题目描述:
  • 示例:
  • 解法:

题目描述:

给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母。

数组里字母的顺序是循环的。举个例子,如果目标字母target = ‘z‘ 并且有序数组为 letters = [‘a‘, ‘b‘],则答案返回 ‘a‘

示例:

输入:
letters = ["c", "f", "j"]
target = "a"
输出: "c"

输入:
letters = ["c", "f", "j"]
target = "c"
输出: "f"

输入:
letters = ["c", "f", "j"]
target = "d"
输出: "f"

输入:
letters = ["c", "f", "j"]
target = "g"
输出: "j"

输入:
letters = ["c", "f", "j"]
target = "j"
输出: "c"

输入:
letters = ["c", "f", "j"]
target = "k"
输出: "c"

注意:

  1. letters长度范围在[2, 10000]区间内。
  2. letters 仅由小写字母组成,最少包含两个不同的字母。
  3. 目标字母target 是一个小写字母。

解法:

class Solution {
public:
    char nextGreatestLetter(vector<char>& letters, char target) {
        int sz = letters.size();
        int l = 0, r = sz-1, mid = 0;
        while(l <= r){
            mid = l + (r - l)/2;
            if(letters[mid] <= target){
                l = mid + 1;
            }else{
                r = mid - 1;
            }
        }
        if(r == -1 || l == sz){
            return letters[0];
        }else{
            return letters[l];
        }
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10619479.html

时间: 2024-10-30 00:35:19

leetcode 744. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than 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 找到大于目标的最小的字母

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] 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.寻找比目标字母大的最小字母(Java 二分查找 easy)

https://leetcode-cn.com/problems/find-smallest-letter-greater-than-target/submissions/ class Solution { public char nextGreatestLetter(char[] letters, char target) { int n=letters.length; int l=0,h=n-1; while(l<=h){ int mid=l+(h-l)/2; if(letters[mid]

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

寻找大学目标及行动步骤——记ITAEM团队第二期宣讲会(2014.05.14)

·昨晚8:00-9:40,在 钟海楼03029 ,进行了ITAEM团队第二期宣讲会(第一期见第一期宣讲会总结),来参加的主要是大一学生,以信院为主,也有法学院.文学院的同学.在宣讲会中,大家都比较积极认真. 第二期宣讲会议程 (1)ITAEM团队骆宏作"有目标,才有奋斗的动力"主题分享,时间约为晚8点-8点40分: (2)丁又专作"Doing,Now(现在就行动)"演讲,时间约为晚8点45分-9点28分: (3)ITAEM团队温辉翔分享自己技术成长经验与推荐书籍,时

c++实验5--统计输出字符串中(大/小写)字母个数,数字个数及其它字符个数。

一.问题及代码 /* * 文件名称: * 作 者: 杨楚莛 * 完成日期: 2016 年 5 月 3 日 * 版 本 号:v1.0 * 对任务及求解方法的描述部分:统计输出字符串中(大/小写)字母个数,数字个数及其它字符个数. * 输入描述: * 问题描述: * 程序输出: * 问题分析: * 算法设计: */ #include<iostream> #include<cstdio> using namespace std; int main() { char str[50]; in

【leetcode 简单】 第七十题 有效的字母异位词

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" 输出: true 示例 2: 输入: s = "rat", t = "car" 输出: false 说明: 你可以假设字符串只包含小写字母. 进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? class Solution: