LeeCode Contest 62 Find Smallest Letter Greater Than Target
来源:互联网 发布:部落冲突女王升级数据 编辑:程序博客网 时间:2024/06/05 09:12
Weekly Contest 62 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’.“
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”
### 问题分析这道题是一道比较简单的问题,求以排序数组中大于目标值中的候选元素中的最小值。直接从后向前遍历,如果出现小于目标值的,说明上一个元素大于目标值。这里有一点说数组算是开始后最后连在一起了,所以没有大于目标值的,那就返回第一个元素。如果都比目标值大,自然也返回第一个元素## 代码如下``` javapublic char nextGreatestLetter(char[] letters, char target) { for (int i = letters.length-1; i >=0; i--) { //从后向前遍历整个数组 if(letters[i]<=target){ //如果大于target大于或等于当前的元素,说明前一个元素是符合要求的,这里需要考虑循环的问题 if(i==letters.length-1) return letters[0]; else return letters[i+1]; } } //如果遍历完都没有返回,就返回第一个元素 return letters[0]; }
LeetCode学习笔记持续更新
GitHub地址 https://github.com/yanqinghe/leetcode
CSDN博客地址 http://blog.csdn.net/yanqinghe123/article/category/7176678
- LeeCode Contest 62 Find Smallest Letter Greater Than Target
- Find Smallest Letter Greater Than Target(leetcode)
- 744. Find Smallest Letter Greater Than Target
- LWC 62:744. Find Smallest Letter Greater Than Target
- Find Smallest Letter Greater Than Target问题及解法
- LeetCode之Find Smallest Letter Greater Than Target(Kotlin)
- LeetCode刷题 | 744. Find Smallest Letter Greater Than Target | 简单 循环题
- LeetCode 744. Find Smallest Letter Greater Than Target (时间复杂度O(n))
- Smallest subarray with sum greater than a given value
- LeetCode Weekly Contest 56 Find K-th Smallest Pair Distance
- [LinkedIn] Smallest character that is strictly larger than the search character target
- Whole is Greater than Sum of Parts
- find the th smallest
- find Kth smallest
- Find the missing letter
- leecode 378. Kth Smallest Element in a Sorted Matrix java
- leecode 230. Kth Smallest Element in a BST
- leecode 解题总结:17. Letter Combinations of a Phone Number
- Spring中的线程安全
- python将mysql数据库的数据转换为json数据
- C++问题零碎
- Mysql : Mysql索引
- CentOS7使用yum安装MySQL
- LeeCode Contest 62 Find Smallest Letter Greater Than Target
- 计算1->n中数字x出现的次数
- 最全各类排序算法原理及其C语言实现
- 大小端,汇编指令,if和swith的区别,左移右移等
- 动态规划问题
- struts2的核心和工作原理
- #make及makefile
- JVM性能调优之生成堆的dump文件
- 面试前准备