首页 > 其他 > 详细

1394. Find Lucky Integer in an Array

时间:2020-06-29 20:35:28      阅读:69      评论:0      收藏:0      [点我收藏+]

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

 

1394. Find Lucky Integer in an Array

原文:https://www.cnblogs.com/whatyouthink/p/13209920.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!