[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 = ‘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:

  1. letters has a length in range [2, 10000].
  2. letters consists of lowercase letters, and contains at least 2 unique letters.
  3. target is a lowercase letter.


给定一个只包含小写字母的有序数组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 是一个小写字母。


Runtime: 220 ms

Memory Usage: 19.8 MB

 1 class Solution {
 2     func nextGreatestLetter(_ letters: [Character], _ target: Character) -> Character {
 3         if target >= letters.last!
 4         {
 5             return letters[0]
 6         }
 7         var n:Int = letters.count
 8         var left:Int = 0
 9         var right:Int = n
10         while (left < right)
11         {
12             var mid:Int = left + (right - left) / 2
13             if letters[mid] <= target
14             {
15                 left = mid + 1
16             }
17             else
18             {
19                 right = mid
20             }
21         }
22         return letters[right]
23     }
24 }


252ms

 1 class Solution {
 2     func nextGreatestLetter(_ letters: [Character], _ target: Character) -> Character {
 3     var left = 0
 4     var right = letters.count
 5     while left < right {
 6         let temp = (left + right) / 2
 7         if letters[temp] > target{
 8             right = temp
 9         }else{
10             left = temp + 1
11         }
12     }
13
14     return letters[left % letters.count]
15     }
16 }


272ms

 1 class Solution {
 2     func nextGreatestLetter(_ letters: [Character], _ target: Character) -> Character {
 3
 4             if letters.isEmpty {
 5             return target
 6         }
 7
 8         guard let codeTarget = target.unicodeScalars.first?.value else {
 9             return target
10         }
11
12         guard let codeLettersLast = letters[letters.count - 1].unicodeScalars.first?.value else {
13             return target
14         }
15
16         if codeLettersLast <= codeTarget {
17
18             return letters[0]
19
20         } else {
21
22             for letter in letters {
23                 guard let code = letter.unicodeScalars.first?.value else {
24                     continue
25                 }
26
27                 if code > codeTarget {
28                     return letter
29                 }
30             }
31
32         }
33
34         return target
35     }
36 }

 1 class Solution {
 2     func nextGreatestLetter(_ letters: [Character], _ target: Character) -> Character {
 3         for letter in letters{
 4             if (letter > target){
 5                 return letter
 6             }
 7         }
 8         return letters[0]
 9     }
10
11 }


284ms

 1 class Solution {
 2     func nextGreatestLetter(_ letters: [Character], _ target: Character) -> Character {
 3         let zValue = "z".unicodeScalars.first!.value
 4         var targetValue = target.unicodeScalars.first!.value
 5         var minV:UInt32 = zValue + 1
 6         var minC:Character?
 7         for letter in letters{
 8             let letterValue = letter.unicodeScalars.first!.value
 9             if letterValue > targetValue,
10             letterValue < minV{
11                 minV = letterValue
12                 minC = letter
13             }
14         }
15         return minC ?? letters[0]
16     }
17 }


288ms

 1 class Solution {
 2     func nextGreatestLetter(_ letters: [Character], _ target: Character) -> Character {
 3         var answer:Character = letters[0]
 4
 5         for (index,letter) in letters.enumerated(){
 6             if letter <= target{
 7                 if index < letters.count - 1{
 8                 answer = letters[index + 1]}
 9                 if index == letters.count - 1{
10                     answer = letters[0]
11                 }}
12         }
13
14         return answer
15     }
16 }

原文地址:https://www.cnblogs.com/strengthen/p/10525089.html

时间: 2024-10-29 09:23:29

[Swift]LeetCode744. 寻找比目标字母大的最小字母 | Find Smallest Letter Greater Than Target的相关文章

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

目录 题目描述: 示例: 解法: 题目描述: 给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 数组里字母的顺序是循环的.举个例子,如果目标字母target = 'z' 并且有序数组为 letters = ['a', 'b'],则答案返回 'a'. 示例: 输入: letters = ["c", "f", "j"] target = "a" 输出: &quo

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]

Swift游戏开发实战教程(大学霸内部资料)

Swift游戏开发实战教程(大学霸内部资料) 试读下载地址:http://pan.baidu.com/s/1sj7DvQH 介绍:本教程是国内第一本Swift游戏开发专向资料. 本教程详细讲解记忆配对.太空侵略者.Simon记忆.迷你高尔夫.银河大战五个游戏的开发.在项目讲解同时,还着详细介绍了图形绘制.游戏引擎.音频引擎.用户交互.传感器等专向技术.最后,教程讲解苹果专用游戏框架Sprit Kit的使用.为了帮助读者充分了解实际开发,教程还详细讲解游戏开发的必备知识,如帐号绑定.发布游戏等内容

寻找大学目标及行动步骤——记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

输出一个等边三角形的字母阵,等边三角形的两腰为字母A,往里靠依次字母大一个(详细题目文章中描述)

题目简单的描述就是输出这么一个金字塔型的字母阵(等边三角形) /* A ABA ABCBA */ /* //解法①:对称轴法 #import <stdio.h> int main() { int i,j; for(i=0;i<3;i++) { for(j=0;j<2-i;j++) //先把空格输出够 printf(" "); for(j=-i;j<=i;j++) //在已经输出完的空格右边输出字母,第一行就是从-0-+0,共一个字母:第二行就是从-1-+1

字母大变小,小变大

import java.util.Scanner; /** * 8.分析以下需求,并用代码实现: * 从键盘循环录入录入一个字符串,输入"end"表示结束 * 将字符串中大写字母变成小写字母,小写字母变成大写字母, * 其它字符用"*"代替,并统计字母的个数 * 键盘录入:Hello12345World * 输出结果:hELLO*****wORLD */public class Test08 { public static void main(String[] ar

Android中ListView字母排序,实现字母挤压效果以及右侧快速选中字母,搜索关键字功能

Android中ListView字母排序,实现字母挤压效果以及右侧快速选中字母,搜索关键字功能 本文中阐述如何自定义EditText实现搜索框自定义的样式以及挤压字母的思路等 自定义EditText 相关的drawable文件 主界面以及相关的适配器 结果展示 定义要呈现的EditText的样式 public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { /** * 定义删