Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 1.82 KB

274. H-Index.md

File metadata and controls

60 lines (46 loc) · 1.82 KB

274. H-Index

Problem

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.

tag:

Solution

这题重点在于理解H-Index(H指数)的概念,即一个人所发表的文章的中有N篇被引用了至少N次,则他的H指数就是N。

求解方法:

  1. 首先对数组降序排列
  2. 从前往后查找排序后的列表,直到某篇论文的序号大于该论文的被引次数。

java

    public int hIndex(int[] citations) {
        quickSort(citations, 0, citations.length-1);
        for(int i=1; i<=citations.length; i++){
            if(i>citations[i-1]) return i-1;
        }
        return citations.length;
    }
    
    // 从高到底排序
    void quickSort(int[] nums, int l, int r) {
        if(l<r) {
            int q = partion(nums, l, r);
            quickSort(nums, l, q-1);
            quickSort(nums, q+1, r);
        }
    }
    
    int partion(int[] nums, int l, int r) {
        int x = nums[r];
        int i = l;
        for(int j=l; j<r; j++) {
            if(nums[j]>x) swap(nums, i++, j);
        }
        swap(nums, i, r);
        return i;
    }
    
    void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

go