您的位置:首页 > 其它

leetcode745: Find Smallest Letter Greater Than Target

2018-01-03 19:23 507 查看
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:

letters
 has a length in range 
[2,
10000]
.
letters
 consists of lowercase letters, and contains at least 2 unique letters.
target
 is a lowercase letter.

直接解就ok

public char nextGreatestLetter(char[] letters, char target) {
int j = 0;
for (int i = 0; i < letters.length; i++) {
if (letters[i] > target) {
j = i;
break;
}
}
System.out.println(j);
if (j == letters.length)
return letters[0];
else
return letters[j];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: