Very Concise and Easy Code


  • 0
    Y
    public class Solution {
        public int hIndex(int[] citations) {
            //corner case
            if(citations==null||citations.length==0) {
                return 0;
            }
            
            //sort it first
            Arrays.sort(citations);
            
            //then, from right to left, compare the index with current value
            int len = citations.length;
            for (int i = 0; i < len; i++) {
                if (citations[len-i-1] < i+1) {
                    return i;
                }
            }
            return len;
        }
    }
    

Log in to reply
 

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