【LeetCode每日一题】——274.H指数

一【题目类别】

  • 排序

二【题目难度】

  • 中等

三【题目编号】

  • 274.H指数

四【题目描述】

  • 给你一个整数数组 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 = = c i t a t i o n s . l e n g t h n == citations.length n==citations.length
  • 1 < = n < = 5000 1 <= n <= 5000 1<=n<=5000
  • 0 < = c i t a t i o n s [ i ] < = 1000 0 <= citations[i] <= 1000 0<=citations[i]<=1000

七【解题思路】

  • 首先对数组从大到小排序
  • 因为我们要求H指数的最大值,所以从后向前遍历,因为已经排序,数组越往后面值越大
  • 初始化定义H指数为0,因为此时还没开始遍历,就说明还没有论文,也没有引用量
  • 然后如果数组的当前值大于H指数,说明已找到了“一篇文章”的引用量大于H指数,那么就让H指数增加一
  • 遍历完成数组后,返回结果即可

八【时间频度】

  • 时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn) n n n为传入的数组的长度
  • 空间复杂度: O ( l o g n ) O(logn) O(logn) n n n为传入的数组的长度

九【代码实现】

  1. Java语言版
class Solution {
    public int hIndex(int[] citations) {
        Arrays.sort(citations);
        int h = 0;
        int n = citations.length - 1;
        for(int i = n; i >= 0 && citations[i] > h;i--){
            h++;
        }
        return h;
    }
}
  1. C语言版
int compare(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

int hIndex(int* citations, int citationsSize)
{
    qsort(citations, citationsSize, sizeof(int), compare);
    int h = 0;
    int n = citationsSize - 1;
    for(int i = n;i >= 0 && citations[i] > h;i--)
    {
        h++;
    }
    return h;
}
  1. Python语言版
class Solution:
    def hIndex(self, citations: List[int]) -> int:
        citations.sort()
        h = 0
        i = len(citations) - 1
        while i >= 0 and citations[i] > h:
            h += 1
            i -= 1
        return h
  1. C++语言版
class Solution {
public:
    int hIndex(vector<int>& citations) {
        sort(citations.begin(), citations.end());
        int h = 0;
        int n = citations.size() - 1;
        for(int i = n; i >= 0 && citations[i] > h;i--){
            h++;
        }
        return h;
    }
};

十【提交结果】

  1. Java语言版
    在这里插入图片描述

  2. C语言版
    在这里插入图片描述

  3. Python语言版
    在这里插入图片描述

  4. C++语言版
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IronmanJay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值