zoukankan      html  css  js  c++  java
  • 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 = '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"

    给定一个只包含小写字母的已排序字符的列表,并给出目标字母目标,找到列表中比给定目标大的最小元素。

    字母是可以环绕的。例如,如果target是target ='z',letters = ['a','b'],则答案是'a'。

    1. /**
    2. * @param {character[]} letters
    3. * @param {character} target
    4. * @return {character}
    5. */
    6. var nextGreatestLetter = function(letters, target) {
    7.   for (let i in letters) {
    8.        if (letters[i] > target) {
    9.            return letters[i];
    10.        }
    11.    }
    12.    return letters[0];
    13. };






  • 相关阅读:
    Git更新或提交出错的解决办法
    webpack简单学习的入门教程
    CentOS源码安装QT
    后台程序在向tty/串口写数据的时候stop了
    Linux signal 处理
    Linux C 获取 文件的大小
    Microsoft Excel 标题栏或首行锁定
    Socket连接何时需要断开
    Windows MFC 打开文本
    动态库的生产和调用
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/8025388.html
Copyright © 2011-2022 走看看