LeetCode OJ 之 H-Index (H指数)

题目:

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.

有一个研究员的论文的引用数组,写一个函数计算研究员的h指数。

h指数是指:N篇论文中有h篇至少有h次引用,其他N-h篇论文不超过(小于等于)h次引用(取满足条件的最大的h值)。

思路:

1、先排序,判断如果有数字满足citations[i] >= len-i 则它满足条件。意思是至少有len-i篇论文引用次数不低于len-i。

比如1,2,3,4,5,6,则结果是3,至少有3篇论文引用次数大于等于3,其他3篇论文引用次数小于等于3.

2、O(n)的解法。见下面代码。

代码1:

[cpp]  view plain copy
  1. class Solution {  
  2. public:  
  3.     int hIndex(vector<int>& citations)   
  4.     {  
  5.         sort(citations.begin() , citations.end());  
  6.         int len = citations.size();  
  7.         for(int i = 0 ; i < len ; i++)  
  8.         {  
  9.             if(citations[i] >= len - i)  
  10.                 return len - i;  
  11.         }  
  12.         return 0;  
  13.     }  
  14. };  

代码2:

[cpp]  view plain copy
  1. class Solution {  
  2. public:  
  3.     int hIndex(vector<int>& citations)   
  4.     {  
  5.         if(citations.empty())  
  6.             return 0;  
  7.         int len = citations.size();  
  8.         vector<int> help(len+1 , 0);    //help[len]用来统计引用次数超过len的次数  
  9.         for(int i = 0 ; i < len ; i++)  
  10.         {  
  11.             if(citations[i] >= len)  
  12.                 help[len]++;  
  13.             else  
  14.                 help[citations[i]]++;  
  15.         }  
  16.         int count = 0;  
  17.         for(int i = len ; i >= 0 ; i--)  
  18.         {  
  19.             count += help[i];//count保存的是引用次数超过某个数字的论文数量  
  20.             //count >= i表示引用次数超过i的论文数量  
  21.             if(count >= i)       
  22.                 return i;  
  23.         }  
  24.         return 0;  
  25.     }  
  26. };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值