Share my Recursive JAVA solution


  • 0
    W
    public class Solution {
        public int hIndex(int[] citations) {
            if (citations.length == 0) return 0;
            return find(0, citations.length - 1, citations);
        }
        private int find(int start, int end, int[] nums) {
            if (start > end) return nums.length - start;
            int midIndex = (end + start) / 2;
            int citations = nums[midIndex];
            int num = nums.length - midIndex;
            if (citations < num) {
                return find(midIndex + 1, end, nums);
            } else if (citations > num) {
                return find(start, midIndex - 1, nums);
            } else {
                return num;
            }
        }
    }

Log in to reply
 

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