leetcode745: Find Smallest Letter Greater Than Target
2018-01-03 19:23
507 查看
Given a list of sorted characters
a target letter
given target.
Letters also wrap around. For example, if the target is
Examples:
Note:
直接解就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];
}
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 = '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:
lettershas a length in range
[2, 10000].
lettersconsists of lowercase letters, and contains at least 2 unique letters.
targetis 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];
}
相关文章推荐
- LeetCode 744 Find Smallest Letter Greater Than Target
- [LeetCode] Find Smallest Letter Greater Than Target 找比目标值大的最小字母
- Find Smallest Letter Greater Than Target(leetcode)
- LeetCode之Find Smallest Letter Greater Than Target(Kotlin)
- Find Smallest Letter Greater Than Target
- Find Smallest Letter Greater Than Target
- LeeCode Contest 62 Find Smallest Letter Greater Than Target
- Find Smallest Letter Greater Than Target问题及解法
- Find Smallest Letter Greater Than Target
- BinarySearch-744-Find Smallest Letter Greater Than Target
- leetcode744: Find Smallest Letter Greater Than Target
- LeetCode-Find K Pairs with Smallest Sums
- leetcode_373 Find K Pairs with Smallest Sums
- LeetCode Find K Pairs with Smallest Sums
- [leetcode] 373. Find K Pairs with Smallest Sums 解题报告
- [LeetCode] Search for a Range (sorted integers array,find start & end position of a target number)
- Smallest subarray with sum greater than a given value
- Two Sum - Greater than target
- [Leetcode] 373. Find K Pairs with Smallest Sums 解题报告
- leetcode Find K Pairs with Smallest Sums