016— H指数【LeetCode274】

吃猫的鱼
2023-07-31 / 0 评论 / 139 阅读 / 正在检测是否收录...

题目

274. H 指数 - 力扣(LeetCode)

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数

根据维基百科上 h 指数的定义h 代表“高引用次数” ,一名科研人员的 h 指数 是指他(她)至少发表了 h 篇论文,并且每篇论文 至少 被引用 h 次。如果 h 有多种可能的值,h 指数 是其中最大的那个。

示例 1:

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

示例 2:

输入:citations = [1,3,1]
输出:1

提示:

  • n == citations.length
  • 1 <= n <= 5000
  • 0 <= citations[i] <= 1000

解题

解法一

思路

首先数组进行排序,然后从第一个开始遍历,要是citations[i] <= citations.length - i,就更新max,遍历结束,返回max即可完成。

解决

class Solution {
    public int hIndex(int[] citations) {
        Arrays.sort(citations);
        int max = 0;
        for(int i=0;i<citations.length;i++){
            if(citations[i] <=  citations.length - i){
                max = Math.max(max,citations[i]);
            }else{
                max = Math.max(max,citations.length - i);
            }
        }
        return max;
    }
}

优化

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

结果

> 2023/07/29 11:58:43    
解答成功:
    执行耗时:1 ms,击败了84.59% 的Java用户
    内存消耗:39.2 MB,击败了82.43% 的Java用户

0

评论 (0)

取消
友情链接 文章阅读: 网站地图