A very simple solution using sort in Java


  • 0
    H
    public int hIndex(int[] citations) {
        if (citations == null || citations.length < 2) {
            return citations == null || citations.length == 0? 0: citations[0] == 0? 0: 1;
        }
        Arrays.sort(citations);
        int index = 1;
        while (index <= citations.length) {
            if (citations[citations.length - index] >= index) {
                index += 1;
            } else {
                break;
            }
        }
        return index - 1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.