leetcode:(274)H-Index(java)

package LeetCode_HashTable;


/**
 * 题目:
 *      Given an array of citations (each citation is a non-negative integer) of a researcher,
 *      write a function to compute the researcher's h-index.
 *      According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her
 *      N papers have at least h citations each, and the other N − h papers have no more than h citations each."
 *      Example:
 *          Input: citations = [3,0,6,1,5]
 *          Output: 3
 *          [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.
 *          Since the researcher has 3 papers with at least 3 citations each and the remaining two with
 *          no more than 3 citations each, her h-index is 3.
 *          (3,0,6,1,5]表示研究人员共有5篇论文,每篇论文分别获得3次,0次,6次,1次5次引用。由于研究人员有3篇论文,每篇论文至少引用3篇,
 *          其余两个每个引用次数不超过3次,她的h指数为3。)
 *  题目大意:
 *      给定研究者的引用数组(每个引用是非负整数),编写一个函数来计算研究者的h指数。
 *      根据维基百科上h-index的定义:“如果他/她的N篇论文中至少有h引文,那么科学家就有索引h,其他的N-h论文每篇都不超过引       数。”
 * 解题思路:
 *      根据给出的数组citations创建一个新的数组array,使新数组的索引代表论文被引用的次数,数组值代表引用次数出现的次数。
 *      然后从新数组的末尾开始向数组的头部开始遍历,在每次遍历时,将数组值累加求和sum,若出现所求和大于数组索引,
 *      则此时的数组索引即为所求的h指数。
 */
public class HIndex_274_1022 {
    public int HIndex(int[] citations) {
        if (citations == null || citations.length == 0) {
            return 0;
        }

        int length = citations.length;
        int[] array = new int[length + 1];
        for (int i = 0; i < length; i++) {
            if (citations[i] >= length) {
                array[length] += 1;
            } else array[citations[i]] += 1;
        }

        int result ; 
        int num = 0;
        for (result = length; result >= 0; result--) {
            num += array[result];
            if (num >= result) {
                return result;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        int[] array = {3, 0, 6, 1, 5};
        HIndex_274_1022 test = new HIndex_274_1022();
        int result = test.HIndex(array);
        System.out.println(result);
    }
}

猜你喜欢

转载自blog.csdn.net/Sunshine_liang1/article/details/83269134