C#LeetCode刷题之#744-寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4001 访问。
给定一个只包含小写字母的有序数组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"
注:
- letters长度范围在[2, 10000]区间内。
- letters 仅由小写字母组成,最少包含两个不同的字母。
- 目标字母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"
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.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4001 访问。
public class Program {
public static void Main(string[] args) {
var letters = new char[] { 'c', 'f', 'j' };
var target = 'd';
var res = NextGreatestLetter(letters, target);
Console.WriteLine(res);
letters = new char[] { 'a', 'c', 'f', 'j' };
target = 'b';
res = NextGreatestLetter2(letters, target);
Console.WriteLine(res);
Console.ReadKey();
}
private static char NextGreatestLetter(char[] letters, char target) {
//暴力法
foreach(var c in letters) {
if(c > target) return c;
}
return letters[0];
}
private static char NextGreatestLetter2(char[] letters, char target) {
//二分法
var low = 0;
var high = letters.Length - 1;
var mid = 0;
while(low <= high) {
mid = low + (high - low) / 2;
if(letters[mid] > target) {
high = mid - 1;
} else if(letters[mid] <= target) {
low = mid + 1;
}
}
if(low >= letters.Length) low = 0;
return letters[low];
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4001 访问。
f
c
分析:
显而易见,NextGreatestLetter 的时间复杂度为: ,NextGreatestLetter2 的时间复杂度为:
。
C#LeetCode刷题之#744-寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)的更多相关文章
- C#版(击败100.00%的提交) - Leetcode 744. 寻找比目标字母大的最小字母 - 题解
C#版 - Leetcode 744. 寻找比目标字母大的最小字母 - 题解 744.Find Smallest Letter Greater Than Target 在线提交: https://le ...
- Leetcode之二分法专题-744. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)
Leetcode之二分法专题-744. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target) 给定一个只包含小写字母的有序数组letters ...
- Java实现 LeetCode 744 寻找比目标字母大的最小字母(二分法)
744. 寻找比目标字母大的最小字母 给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 在比较时,数组里字母的是循环有序的.举个例 ...
- [Swift]LeetCode744. 寻找比目标字母大的最小字母 | Find Smallest Letter Greater Than Target
Given a list of sorted characters letterscontaining only lowercase letters, and given a target lette ...
- 【Leetcode_easy】744. Find Smallest Letter Greater Than Target
problem 744. Find Smallest Letter Greater Than Target 题意:一堆有序的字母,然后又给了一个target字母,让求字母数组中第一个大于target的 ...
- Leetcode744.Find Smallest Letter Greater Than Target寻找比目标字母大的最小字母
给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母. 数组里字母的顺序是循环的.举个例子,如果目标字母target = 'z' 并且有 ...
- LeetCode 744. Find Smallest Letter Greater Than Target (寻找比目标字母大的最小字母)
题目标签:Binary Search 题目给了我们一组字母,让我们找出比 target 大的最小的那个字母. 利用 binary search,如果mid 比 target 小,或者等于,那么移到右半 ...
- LeetCode算法题-Find Smallest Letter Greater Than Target(Java实现)
这是悦乐书的第306次更新,第326篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第175题(顺位题号是744).给定一个仅包含小写字母的有序字符数组,并给定目标字母目标 ...
- 744. 寻找比目标字母大的最小字母--LeetCode
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/find-smallest-letter-greater-than-target 著作权归领扣网络所有. ...
随机推荐
- DelayQueue源码分析
DelayQueue<E>继承于AbstractQueue<E>实现BlockingQueue<E> 内部变量包括ReentrantLock 类型的lock以及条件 ...
- oracle终止用户会话
1.创建两个测试用户进行实验 执行命令如下: create user test1 identified by 1; create user test2 identified by 1; grant d ...
- Python Ethical Hacking - Malware Analysis(4)
DOWNLOAD_FILE Download files on a system. Once packaged properly will work on all operating systems. ...
- C++算法 广搜
有一个同学推荐我写一下广搜,广搜在最短路(骗分)上确实也有突出贡献,普及组应该也会考到,我今天就给要考普及组的同学讲讲课,今天讲广搜. 广搜,把可以走到的地点存进队列,然后一个个走,所以他第一次走到一 ...
- 在 vue 中使用 WebSocket
<template> <div class="hello"> <h1>{{ msg }}</h1> <h1>{{ res ...
- java io流根据url读取图片
//获取图片大小 public void readFileSize(String url,HttpServletRequest request){ //根路径 File file = new File ...
- js 判断传入参数是域名还是地址
var get = function(url) { if(location.protocol === "http") { return url; } var reg = /^(ht ...
- jenkins集群(二)(master --> slave) -- allure自动化测试报告部署
一.前提 1.环境 1)已经部署好了jenkins环境,包括jenkins的“全局工具配置”也配好了. 2.master与slave的简单的概念 1)master:jenkins部署所在的机器 2)s ...
- 网络通信机制:Socket、TCP/IP、HTTP
13.1.1 TCP/IP协议 讲的很抽象,没具体看懂什么是TCP协议,什么是IP协议.IP协议保证消息从一个主机传送到另一个主机,消息在传送的过程中被分割成一个个小包,TCP协议会让两台相互连接的计 ...
- R 数据读取与写入
路径 getwd() #获取当前工作路径 setwd() #设置工作路径 获取普通文本数据 x = read.table("data.txt") #通过路径直接获取 x = rea ...