Simple C++ implementation


  • 1
    T
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            int n = citations.size();
            if(n==0)
                return 0;
            sort(citations.begin(), citations.end());
            int max = citations[n-1];
            for(int h=n; h>0; h--)
            {
                if(citations[n-h]>=h)
                    return h;
            }
            return 0;
        }
    };

  • 0
    C

    bool compare(int i, int j) {
    return i > j;
    }

    class Solution {
    public:
    int hIndex(vector<int>& citations) {
    int hIdx = 0;
    vector<int>::const_iterator ite;

        sort(citations.begin(), citations.end(), compare);
        for (ite = citations.begin(); ite != citations.end(); ite++) {
        	if (*ite >= hIdx+1) 
        		hIdx++;
        	else
        		break;
        }
        return hIdx;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.