Wednesday, December 2, 2015

[leetcode]H-index

这题有点绕啊 别想太多 就用wiki的方法 sort好 从大到小看... 假如有index >= collection[index]的 那就return index, 最后return size
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class Solution {
    public int hIndex(int[] citations) {
        if (citations == null || citations.length == 0) return 0;
        int[] copy = Arrays.copyOf(citations, citations.length);
        Arrays.sort(copy);
        for (int i = copy.length-1, j = 0; i >=0; i--, j++){
            if (j>=copy[i]) return j;
        }
        return copy.length;
    }
}

No comments:

Post a Comment