1394. Find Lucky Integer in an Array

Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value.

Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky integer return -1.

找出出现个数等于数字的这个数,有多个的话给出最大值

用一个count记录所有的数出现的次数,然后倒序寻找最大的数

class Solution(object):
    def findLucky(self, arr):
        """
        :type arr: List[int]
        :rtype: int
        """
        count = [0] * 501
        for value in arr:
            count[value] += 1
        for i in range(500, 0, -1):
            if i == count[i]:
                return i
        return -1

 

posted @ 2020-06-29 19:40  whatyouthink  阅读(50)  评论(0编辑  收藏  举报