[leetcode_274]H-Index

降序排列,然后即可线性查找符合条件的h索引。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
public:
    int hIndex(vector<int>& citations) {
        sort(citations.begin(), citations.end(), greater<int>());
        int size = citations.size();
        int max = 0;
        for (int i = 0; i < size; i++)
        {
            if (citations[i] >= i + 1)
            {
                max = i + 1;
            }
        }
        return max;
    }
};
Licensed under CC BY-NC-SA 4.0