LeetCode H-index and H-index II

题目:

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."

For example, given citations = [3, 0, 6, 1, 5], which 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, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.

Hint:

  1. An easy approach is to sort the array first.
  2. What are the possible values of h-index?
  3. A faster approach is to use extra space.

题意:

给定一个关于论文的引用次数的数组,然后计算关于这个学者的H-index指数。一开始我不太理解这道题目,后来经过网上搜了相关的概念,知道了其实很好理解。

定义如下:

H指数的计算基于其研究者的论文数量及其论文被引用的次数。赫希认为:一个人在其所有学术文章中有N篇论文分别被引用了至少N次,他的H指数就是N。那么具体的计算方法如下:

1、将其发表的所有SCI论文按被引次数从高到低排序;2、从前往后查找排序后的列表,直到某篇论文的序号大于该论文被引次数。所得序号减一即为H指数。

所以首先只要对这个数组中的元素进行排序即可,然后就是就是按照序号和对应的数组中的值比较,如果发现,序号的值比对应数组中的值要大,那么对应的那个序号减一就是H-index的值。这里我采用了一个小技巧,就是在对数组排序的时候,我采用了归并排序,这种算法是刚学,而且对数组进行归并排序,会非常适用,时间复杂度为O(nlogn),会用到递归的思想。

public static int hIndex(int[] citations)
	{
		int length = citations.length;
		sort(citations,0,length - 1);
		/*for(int j = 0; j < length; j++)
		   System.out.println(citations[j]);*/
		int i = 1;
		for(i = 1; i <= length; i++)
		{
		    if(i > citations[i-1])
		       break;
		}
		i = i - 1;
		return i;
	}
	public static void sort(int[] data,int left,int right)
	{
		if(left >= right)
			return;
		int center = (left + right) / 2;
		sort(data,left,center);     //在进行排序的时候,采用递归的方式来做
		sort(data,center + 1,right);
		merge(data,left,center,right);    //最后,合并左右两个数组
	}
	public static void merge(int[] data,int left,int center,int right)
	{
		int[] tmpArr = new int[data.length];
		int mid = center + 1;
		int third = left;
		int tmp = left;
		while(left <= center && mid <= right)
		{
			if(data[left] <= data[mid])
				tmpArr[third++] = data[mid++];
			else
				tmpArr[third++] = data[left++];
		}
		while(mid <= right)
		{
			tmpArr[third++] = data[mid++];
		}
		while(left <= center)
		{
			tmpArr[third++] = data[left++];
		}
		while(tmp <= right)
		{
			data[tmp] = tmpArr[tmp++];
		}
	}
主要是想要练下归并排序算法,两两排序,然后再两两归并。

H-index II:

题目:

现在给的数组是按照引用次数从小到大排序的,那么同理,我只需要将这个i从尾开始循环遍历即可。思路与上一题一样。

public int hIndex(int[] citations)
	{
		int length = citations.length;
		if(length == 1)
		{
			if(citations[0] >= 1)
				return 1;
		}
		int i = 1;
		for(i = 1; i < length; i++)
		{
			if(i > citations[length - i])
				break;
		}
		i = i + 1;
		return i;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值