citations
array is sorted in ascending order? Could you optimize your algorithm?
public class Solution {
public int hIndex(int[] citations) {
if (citations == null || citations.length == 0) {
return 0;
}
int s = 0;
int e = citations.length - 1;
int h = 0;
while (s <= e) {
h = (s + e) / 2;
if (citations[h] >= citations.length - h && (h == 0 || citations[h-1] < citations.length - (h - 1))) {
return citations.length - h;
} else if (citations[h] > citations.length - h) {
e = h - 1;
} else {
s = h + 1;
}
}
return 0;
}
}
没有评论:
发表评论