leetcode-182周赛-5368. 找出数组中的幸运数

题目描述:

 python提交:

from typing import List
import collections
class Solution:
    def findLucky(self, arr: List[int]) -> int:
        dic = collections.Counter(arr)
        ans = -1
        for i,v in dic.items():
            if i == v:
                ans = max(ans,i)
        return ans

java:

class Solution {
    public int findLucky(int[] arr) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int a: arr) {
            map.put(a, map.getOrDefault(a, 0) + 1);
        }
        int res = -1;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getKey() == entry.getValue()) {
                res = Math.max(res, entry.getKey());
            }
        }
        return res;
    }
}

猜你喜欢

转载自www.cnblogs.com/oldby/p/12592224.html