大于给定元素的最小元素
744. Find Smallest Letter Greater Than Target (Easy)
Input:
letters = ["c", "f", "j"]
target = "d"
Output: "f"
Input:
letters = ["c", "f", "j"]
target = "k"
Output: "c"
题目描述:
??给定一个有序的字符数组letters和一个字符target,要求找出letters中大于target的最小字符,如果找不到就返回第一个字符。
思路分析:
??采用二分法进行查找。
代码:
public char nextGreatestLetter(char []letters,char target){
if(letters==null||letters.length==0)
return null;
int len=letters.length;
int l=0,h=n-1;
while(l<=h){
int mid=l+(h-l)/2;
if(letters[mid]<=target){
l=mid+1;
}else{
h=mid-1;
}
}
return l<len?letters[l]:letters[0];
}
原文地址:https://www.cnblogs.com/yjxyy/p/11106138.html
时间: 2024-11-09 15:13:12