Leetcode1207独一无二的出现次数

在这里插入图片描述
java采用HashMap+Set实现,HashMap用于记录每个数出现的次数,之后存到Set中进行判断

class Solution {
    public boolean uniqueOccurrences(int[] arr) {
        Map<Integer,Integer> count = new HashMap<Integer,Integer>();
        for(Integer i : arr){
            if(count.containsKey(i)){
                count.put(i,count.get(i)+1);
            }else{
                count.put(i,1);
            }
        }
        int size = count.size();
        Set<Integer> set = new HashSet<Integer>();
        //map中对于值的遍历
        //对于key是Integer key :map.keys
        for(Integer value:count.values()){
            set.add(value);
        }
        if(set.size() == size){
            return true;
        }
        return false;
    }
}

d.get(i,0) 如果集合中没有值默认赋为0

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        d = {}
        for i in arr:
            d[i] = d.get(i,0) + 1
        s = [d[i] for i in d]
        return len(s)==len(set(s))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值