H-Index
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:
1 | Input: citations = [3,0,6,1,5] |
Note: If there are several possible values for h, the maximum one is taken as the h-index.
题意分析
该题是要从给出的citations数组中分析出一个数字,数组的有大于等于该数字的项数,他们的值大于等于该数字。思路就是构造一个citations出现次数数组,即代码中的aux,我们再从后向前遍历该数组,每次遍历加上citation出现的次数,若某轮出现累计的次数和大于等于循环变量i,那么我们就找到了这个数字。否则不存在这样的数字,返回0。只涉及到数组单层遍历,时间复杂度为O(N)。
实现代码
1 | class Solution { |