LeetCode 274. H-Index

Problem

题目链接

Notes

这个题的意思是,给一个具有n个非负数的数组,找出最大的h,使得这个数组中h个元素>=h,剩下的n-h个元素<=h。注意两边都有等号(开始做的时候看漏了一个等号,还感觉题目出错了…)
只想到快排顺序查找的方法。时间复杂度就是快排的o(nlogn),空间复杂度o(1)。
遍历时的条件,自己写一下样例就明白了。

Codes

class Solution {
public:
    int hIndex(vector<int>& citations) {
        int len=citations.size();
        if(!len) return 0;
        sort(citations.begin(),citations.end());
        int ans;
        for(int h=1;h<=len;++h)
        {
            if(citations[len-h]<=h)
                return citations[len-h]==h?h:h-1;
            else
                ans=h;
        }
        return ans;
    }
};

Results

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值