-
Notifications
You must be signed in to change notification settings - Fork 0
/
0744.FindSmallestLetterGreaterThanTarget.js
90 lines (66 loc) · 2.26 KB
/
0744.FindSmallestLetterGreaterThanTarget.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Given a characters array letters that is sorted in non-decreasing order and a character target, return the smallest character in the array that is larger than target.
// Note that the letters wrap around.
// For example, if target == 'z' and letters == ['a', 'b'], the answer is 'a'.
//
// Example 1:
// Input: letters = ["c","f","j"], target = "a"
// Output: "c"
// Example 2:
// Input: letters = ["c","f","j"], target = "c"
// Output: "f"
// Example 3:
// Input: letters = ["c","f","j"], target = "d"
// Output: "f"
// Example 4:
// Input: letters = ["c","f","j"], target = "g"
// Output: "j"
// Example 5:
// Input: letters = ["c","f","j"], target = "j"
// Output: "c"
//
// Constraints:
// 2 <= letters.length <= 104
// letters[i] is a lowercase English letter.
// letters is sorted in non-decreasing order.
// letters contains at least two different characters.
// target is a lowercase English letter.
// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/find-smallest-letter-greater-than-target
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
// 🎨 方法一:直接遍历
// 📝 思路:有序
/**
* @param {character[]} letters
* @param {character} target
* @return {character}
*/
var nextGreatestLetter = function (letters, target) {
for (let letter of letters) {
if (letter.charCodeAt() > target.charCodeAt()) {
return letter
}
}
return letters[0]
};
// 🎨 方法一:二分查找
// 📝 思路:如果存在比target大的最小值,那么位置应该在 left;三种情形,target不在letters范围,target在letters范围但不在letters中,target在letters中
/**
* @param {character[]} letters
* @param {character} target
* @return {character}
*/
var nextGreatestLetter = function (letters, target) {
let left = 0;
let right = letters.length - 1;
while (left <= right) {
let mid = left + ((right - left) >> 2);
if (letters[mid] > target) {
right = mid - 1
} else {
// <= 合并为一种情况
left = mid + 1
}
}
// return left === letters.length ? letters[0] : letters[left]
return letters[left % letters.length] // 🔥
};