744. Find Smallest Letter Greater Than Target 找到大於目標的最小的字母
阿新 • • 發佈:2017-12-12
enum letters scrip ase class 目標 let amp pan .
來自為知筆記(Wiz)
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"
給定一個只包含小寫字母的已排序字符的列表,並給出目標字母目標,找到列表中比給定目標大的最小元素。
字母是可以環繞的。例如,如果target是target =‘z‘,letters = [‘a‘,‘b‘],則答案是‘a‘。
/**
* @param {character[]} letters
* @param {character} target
* @return {character}
*/
var nextGreatestLetter = function(letters, target) {
for (let i in letters) {
if (letters[i] >
target) {return letters[i];
}
}
return letters[0];
};
來自為知筆記(Wiz)
744. Find Smallest Letter Greater Than Target 找到大於目標的最小的字母