274. H-Index

笨方法,写完就睡着了

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        if len(citations) == 0:
            return 0
        lenth = len(citations)
        h = lenth
        while h > 0:
            if self.count(citations, h) >= h:
                return h
            h -= 1
        return 0
    def count(self, citetions, h):
        count = 0
        for c in citetions:
            if c >= h:
                count += 1
        return count
        

如果是排好序就很容易了,代码如下:

class Solution:
    def hIndex(self, citations):
        citations.sort()
        for i in range(len(citations)):
            if len(citations) - i <= citations[i]: return len(citations) - i
        return 0

你可能感兴趣的:(274. H-Index)