I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
Question : In an integer array, if the frequency of occurrence of an integer is equal to its numerical value, we call this integer a "lucky number". Give you an array of integers arr, please find out and return a lucky number from it.
If there are multiple array lucky number, simply return the biggest one.
If there is no lucky number in the array, -1 is returned .
classSolution:deffindLucky(self, arr: List[int])->int:
result =[]for i inrange(len(arr)):if arr.count(arr[i])== arr[i]:
result.append(arr[i])if result ==[]:return-1returnmax(result)# 执行用时 :160 ms, 在所有 Python3 提交中击败了6.10%的用户# 内存消耗 :13.7 MB, 在所有 Python3 提交中击败了100.00%的用户
Algorithm description: Count the number of occurrences of the current element arr[i]in the list . If the value of the element is equal to the number of occurrences, store the element in the list and return the largest element in the list .arrarr.count(arr[i])resultresult