LeetCode每日一题 (48) 1207. 独一无二的出现次数(map字典)

在这里插入图片描述
在这里插入图片描述


class Solution {
    
    
public:
    bool uniqueOccurrences(vector<int>& arr) {
    
    
        unordered_map<int, int> occur;
        for (const auto& x: arr) {
    
    
            occur[x]++;
        }
        unordered_set<int> times;
        for (const auto& x: occur) {
    
    
            times.insert(x.second);
        }
        return times.size() == occur.size();
    }
};

在这里插入图片描述


class Solution {
    
    
public:
    bool uniqueOccurrences(vector<int>& arr) {
    
    
        int count[2002] = {
    
    0}; // 统计数字出现的频率
        for (int i = 0; i < arr.size(); i++) {
    
    
            count[arr[i] + 1000]++;
        }
        bool fre[1002] = {
    
    false}; // 看相同频率是否重复出现 
        for (int i = 0; i <= 2000; i++) {
    
    
            if (count[i]) {
    
     // 非0
                if (fre[count[i]] == false) fre[count[i]] = true;
                else return false;
            }
        }
        return true;
    }
};

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45021180/article/details/109347939