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 = ‘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<char>& letters, char target) {
 4         int left = 0 ;
 5         int right = letters.size() - 1 ;
 6         while(left < right){
 7             int mid = (left+right)>>1 ;
 8             if (target < letters[mid])
 9                 right = mid ;
10             else
11                 left = mid + 1 ;
12         }
13         return target < letters[left] ? letters[left] : letters[0] ;
14     }
15 };
时间: 2024-11-09 14:13:05

744. Find Smallest Letter Greater Than Target的相关文章

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

744. Find Smallest Letter Greater Than Target(大于给定元素的最小元素)(leetcode)

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. 寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)

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

[LeetCode] 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

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

Binary search for the first element greater than target

We all know how to search through an array for an element whose value equals the target value, but how to search for the element that has value greater than the target value? A particularly elegant way of thinking about this problem is to think about

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode题解——算法思想之二分查找

1. 求开方 2. 大于给定元素的最小元素 3. 有序数组的 Single Element 4. 第一个错误的版本 5. 旋转数组的最小数字 6. 查找区间 正常实现 Input : [1,2,3,4,5] key : 3 return the index : 2 public int binarySearch(int[] nums, int key) { int l = 0, h = nums.length - 1; while (l <= h) { int m = l + (h - l) /

二分查找---大于给定元素的最小元素

大于给定元素的最小元素 744. Find Smallest Letter Greater Than Target (Easy) Input: letters = ["c", "f", "j"] target = "d" Output: "f" Input: letters = ["c", "f", "j"] target = "k&qu