博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
744. Find Smallest Letter Greater Than Target
阅读量:5323 次
发布时间:2019-06-14

本文共 1412 字,大约阅读时间需要 4 分钟。

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:

  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.

 

二分查找,找到大于给定目标的列表中最小的元素。

 

C++(19ms):

1 class Solution { 2 public: 3     char nextGreatestLetter(vector
& letters, char target) { 4 int left = 0 ; 5 int right = letters.size()-1 ; 6 while(left < right){ 7 int mid = left + (right-left)/2 ; 8 if (target < letters[mid]) 9 right = mid ;10 else11 left = mid + 1 ;12 }13 if (target < letters[left])14 return letters[left] ;15 else16 return letters[0] ;17 }18 };

 

转载于:https://www.cnblogs.com/mengchunchen/p/8098091.html

你可能感兴趣的文章
字符串比较
查看>>
epoll 技术(转)
查看>>
<转>Shell脚本相关
查看>>
使用FreeMarker加载远程主机上模板文件,比如FTP,Hadoop等(转载)
查看>>
Java的位运算符具体解释实例——与(&amp;)、非(~)、或(|)、异或(^)
查看>>
java 注解 学习
查看>>
[leetcode]403. Frog Jump青蛙过河
查看>>
英语音节知识
查看>>
IEEE 802.15.4协议学习之MAC层
查看>>
AngularJS学习篇(十三)
查看>>
Tableau 学习资料
查看>>
中断和异常
查看>>
lucene 全文检索工具的介绍
查看>>
C# MD5-16位加密实例,32位加密实例
查看>>
无线点餐系统初步构思
查看>>
AJAX
查看>>
前端之CSS
查看>>
List注意点【修改】
查看>>
sqoop导入导出对mysql再带数据库test能跑通用户自己建立的数据库则不行
查看>>
拓扑排序的原理及其实现
查看>>