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 = ‘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相关推荐
- LeetCode 744. Find Smallest Letter Greater Than Target (时间复杂度O(n))
题目 太简单了,直接上代码: class Solution { public:char nextGreatestLetter(vector<char>& letters, char ...
- C#LeetCode刷题之#744-寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4001 访问. 给定一个只包含小写字母的有序数组letters 和 ...
- Leetcode744.Find Smallest Letter Greater Than Target寻找比目标字母大的最小字母
给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 数组里字母的顺序是循环的.举个例子,如果目标字母target = 'z' 并且有 ...
- LeetCode 1061. Lexicographically Smallest Equivalent String
原题链接在这里:https://leetcode.com/problems/lexicographically-smallest-equivalent-string/ 题目: Given string ...
- 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 ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- 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 ...
- Java实现 LeetCode 744 寻找比目标字母大的最小字母(二分法)
744. 寻找比目标字母大的最小字母 给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 在比较时,数组里字母的是循环有序的.举个例 ...
- Leetcode——538. Convert BST to Greater Tree
题目原址 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ 题目描述 Given a Binary Sear ...
最新文章
- Redis 笔记(11)— 文本协议 RESP(单行、多行字符串、整数、错误、数组、空值、空串格式、telnet 登录 redis)
- ueditor集成实例php,Laravel框架集成UEditor编辑器的方法图文与实例详解
- Apache搭建多个站点方法详解
- Centos5搭建vsftpd服务
- mac升级之后safari打不开网页,怎么办?
- 【Qt】仿360安全卫士界面(自定义阴影边框类)
- Socket通信---网络通信学习笔记(一)
- nova— 计算服务
- 用原生PHP做Blog系统-Day01
- Spring实战之Cache
- KafKa基本入门教程
- iOS开发之iOS11、iPhone X、Xcode9 适配指南
- iOS The binary you uploaded was invalid
- Xgboost算法——Kaggle案例
- 三分钟带你快速看懂电子数据取证! | 打击网络犯罪必备知识
- 14款实用项目管理工具,精英必备!
- 【边缘计算】刘阳:边缘计算发展中的若干热点问题及思考
- Facebook币Libra学习-6.发行属于自己的代币Token案例(含源码)
- 基于CNN卷积神经网络实现中文手写汉字识别
- How Does Linphone Toggle SIP Contact?