LeetCode75——Day21

文章目录

一、题目

1207. Unique Number of Occurrences

Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.

Example 1:

Input: arr = [1,2,2,1,1,3]

Output: true

Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.

Example 2:

Input: arr = [1,2]

Output: false

Example 3:

Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]

Output: true

Constraints:

1 <= arr.length <= 1000

-1000 <= arr[i] <= 1000

二、题解

cpp 复制代码
class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        unordered_map<int,int> map;
        unordered_map<int,int> times;
        unordered_map<int,int>::iterator it; 
        int n = arr.size();
        for(int i = 0;i < n;i++){
            map[arr[i]]++;
        }
        for (it = map.begin(); it != map.end();it++){
            if(times[map[it->first]] == 0) times[map[it->first]]++;
            else return false;
        }
        return true;
    }
};
相关推荐
小年糕是糕手19 分钟前
【C++】C++入门 -- 输入&输出、缺省参数
c语言·开发语言·数据结构·c++·算法·leetcode·排序算法
情怀姑娘1 小时前
面试题---------------场景+算法
java·算法·mybatis
chbmvdd1 小时前
week5题解
数据结构·c++·算法
用户12039112947261 小时前
面试官最爱问的字符串反转:7种JavaScript实现方法详解
算法·面试
vir021 小时前
小齐的技能团队(dp)
数据结构·c++·算法·图论
月夜的风吹雨1 小时前
【C++红黑树】:自平衡二叉搜索树的精妙实现
开发语言·c++·红黑树
讨厌下雨的天空1 小时前
Linux信号
linux·运维·c++
Star在努力2 小时前
C语言复习八(2025.11.18)
c语言·算法·排序算法
赖small强2 小时前
【Linux C/C++开发】第26章:系统级综合项目理论
linux·c语言·c++
南山安2 小时前
从反转字符串看透面试官的“内心戏”:你的算法思维到底怎么样?
javascript·算法·面试