H指数

给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。

h 指数的定义: “一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 h 篇论文,分别被引用了至少 h 次,其余的 N - h 篇论文每篇被引用次数不多于 h 次。”

示例:

输入: citations = [3,0,6,1,5]
输出: 3 
解释: 给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
     由于研究者有 3 篇论文每篇至少被引用了 3 次,其余两篇论文每篇被引用不多于 3 次,所以她的 h 指数是 3。
说明: 如果 h 有多种可能的值,h 指数是其中最大的那个。

思路:

可以将数组排序  从前往后遍历找临界值  有h篇引用>=h次 其余n-h篇引用次数<h 其中h篇可以用n-i表示

贴代码:

public static int hIndex(int[] citations) {
		if (citations == null || citations.length == 0) {
			return 0;
		}
		if (citations.length == 1) {  //数组长度为1情况
			if (citations[0] >= 1) {
				return 1;
			} else {
				return 0;
			}
		}
        //数组长度大于等于2  进行排序    h代表有h篇引用>=h次 其余<=h次    其中h篇可以用n-i表示
		Arrays.sort(citations);
		int n = citations.length;
		int res = 0;
		if (citations[0] >= n) {
			res = n;
		} else {

			for (int i = 1; i < n; i++) {
				if (citations[i] >= (n - i) && citations[i - 1] <= (n - i)) {
					res = Math.max(res, n - i);
					break;
				}
			}
		}
		return res;

	}

猜你喜欢

转载自blog.csdn.net/qq_41864967/article/details/84712472