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.

分析:用upper_bound返回第一个大于target的元素所在位置,如果这个位置等于letters.end()说明不存在,则返回letters的第一个值,否则返回it所在位置的元素值即可~

class Solution {
public:char nextGreatestLetter(vector<char>& letters, char target) {auto it = upper_bound(letters.begin(), letters.end(), target);return it == letters.end() ? letters[0] : *it;}
};

LeetCode 744. Find Smallest Letter Greater Than Target相关推荐

  1. LeetCode 744. Find Smallest Letter Greater Than Target (时间复杂度O(n))

    题目 太简单了,直接上代码: class Solution { public:char nextGreatestLetter(vector<char>& letters, char ...

  2. C#LeetCode刷题之#744-寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4001 访问. 给定一个只包含小写字母的有序数组letters 和 ...

  3. Leetcode744.Find Smallest Letter Greater Than Target寻找比目标字母大的最小字母

    给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 数组里字母的顺序是循环的.举个例子,如果目标字母target = 'z' 并且有 ...

  4. LeetCode 1061. Lexicographically Smallest Equivalent String

    原题链接在这里:https://leetcode.com/problems/lexicographically-smallest-equivalent-string/ 题目: Given string ...

  5. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  6. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  7. leetcode 496, 503, 556. Next Greater Element I, II, III | 496, 503, 556. 下一个更大元素 I,II,III(单调栈)

    496. Next Greater Element I https://leetcode.com/problems/next-greater-element-i/ 单调栈问题,参考:https://l ...

  8. Java实现 LeetCode 744 寻找比目标字母大的最小字母(二分法)

    744. 寻找比目标字母大的最小字母 给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 在比较时,数组里字母的是循环有序的.举个例 ...

  9. Leetcode——538. Convert BST to Greater Tree

    题目原址 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ 题目描述 Given a Binary Sear ...

最新文章

  1. Redis 笔记(11)— 文本协议 RESP(单行、多行字符串、整数、错误、数组、空值、空串格式、telnet 登录 redis)
  2. ueditor集成实例php,Laravel框架集成UEditor编辑器的方法图文与实例详解
  3. Apache搭建多个站点方法详解
  4. Centos5搭建vsftpd服务
  5. mac升级之后safari打不开网页,怎么办?
  6. 【Qt】仿360安全卫士界面(自定义阴影边框类)
  7. Socket通信---网络通信学习笔记(一)
  8. nova— 计算服务
  9. 用原生PHP做Blog系统-Day01
  10. Spring实战之Cache
  11. KafKa基本入门教程
  12. iOS开发之iOS11、iPhone X、Xcode9 适配指南
  13. iOS The binary you uploaded was invalid
  14. Xgboost算法——Kaggle案例
  15. 三分钟带你快速看懂电子数据取证! | 打击网络犯罪必备知识
  16. 14款实用项目管理工具,精英必备!
  17. 【边缘计算】刘阳:边缘计算发展中的若干热点问题及思考
  18. Facebook币Libra学习-6.发行属于自己的代币Token案例(含源码)
  19. 基于CNN卷积神经网络实现中文手写汉字识别
  20. How Does Linphone Toggle SIP Contact?

热门文章

  1. BaiduMapsApiDemo报错:请在 DemoApplication.java文件输入正确的授权Key
  2. 使用dom4j来解析相关的xml字符串
  3. mysql基本命令总结
  4. 前端:CheckBox事件函数js
  5. weblogic管理3 - 生产模式下免密码管理配置
  6. 百度编辑器UEditor源码模式下过滤div/style等html标签
  7. ACCESS中执行sql语句
  8. ISAServer***检测及配置
  9. SonicWall:速度修复这些严重的 SMA 100 漏洞
  10. IT巨头埃森哲遭 LockBit 勒索攻击,黑客威胁泄露数据