Find-Smallest-Letter-Greater-Than-Target

题目地址

LeetCode#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.

解题思路

  题目给的数组是已经排好序的,所以我们连排序都不用,直接遍历一遍,找到比target大的就可以赋值并且终止循环直接输出了,由于题目中说明了

Letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'

  就是说如果没有比他大的,我们返回第一个值就可以了。所以我们将res的初始值就设置为数组的第一个值,这样遍历一遍后无论是否找到,res都是正确的值。

解题代码【.CPP】

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
char nextGreatestLetter(vector<char> &letters, char target) {
char res = letters[0];
for (auto r : letters) {
if (r > target) {
res = r;
break;
}
}
return res;
}
};
0%