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 著作权归领扣网络所有. ...
随机推荐
- static关键字有何魔法?竟让Spring Boot搞出那么多静态内部类
生命太短暂,不要去做一些根本没有人想要的东西.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习 ...
- Ethical Hacking - POST EXPLOITATION(3)
Spying - Capturing Key Strikes & Taking Screen Shots Log all mouse/keyboard events > keyscan- ...
- 题解 洛谷 P3734 【[HAOI2017]方案数】
可以先考虑没有障碍物的情况,设计状态\(f_{i,j,k}\),表示到达坐标 \((x,y,z)\)二进制下,\(x\)有\(i\)位,\(y\)有\(j\)位,\(z\)有\(k\)位的方案数. 得 ...
- Springboot(二)springboot之jsp支持
参考恒宇少年的博客:https://www.jianshu.com/p/90a84c814d0c springboot内部对jsp的支持并不是特别理想,而springboot推荐的视图是Thymele ...
- 16 . Go之网络编程
互联网的本质 两台计算机之间的通信与两个人打电话原理是一样的. # 1. 首先要通过各种物理连接介质连接 # 2. 找准确对方计算机(准确到软件)的位置 # 3. 通过统一的标准(一般子协议)进行数据 ...
- Redis在Linux下的安装
一.下载地址 ①redis中文网下载地址:http://www.redis.cn/ ②百度云网盘下载地址:https://pan.baidu.com/s/1UQcF9V3lwA0fxquM_JFMZw ...
- IO—》File类
IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再把这些数据 ...
- ken桑带你读源码 之 scrapy_redis
首先更大家说下 正式部署上线的爬虫会有分布式爬虫的需求 而且原本scrapy 的seen (判断重复url的池 不知道用啥词 已抓url吧 ) 保存在磁盘 url 队列 也是保存在磁盘 (保 ...
- 自定义placeholder样式
::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color: #909; } :-moz-placeholder { /* Mozill ...
- ImportError: /lib64/libm.so.6: version `GLIBC_2.23' not found (required by /usr/local/python37/lib/python3.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
一 问题背景 这个错误的出现往往与我前面的一篇文章 ImportError: /lib64/libm.so.6: version `CXXAB_1.3.8.' not found (required ...