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;
    }
};
相关推荐
知无不研1 分钟前
c++垃圾回收机制
开发语言·c++·智能指针·raii·垃圾回收机制
luckycoding3 分钟前
1487. 保证文件名唯一
数据结构·算法·leetcode
DeeGLMath8 分钟前
从基础算法到机器学习的研究轨迹
人工智能·算法·机器学习
Barkamin12 分钟前
冒泡排序的简单实现
java·算法·排序算法
_dindong12 分钟前
【单调栈/队列&并查集&字符串哈希&Tire树】习题集锦
数据结构·c++·算法·哈希算法
西装没钱买15 分钟前
QT组播的建立和使用(绑定特定的网卡,绑定特定IP)
网络·c++·qt·udp·udp组播
独自破碎E22 分钟前
【手撕真题】合并区间
算法
big_rabbit050224 分钟前
[算法][力扣110]平衡二叉树
数据结构·算法·leetcode
二年级程序员31 分钟前
排序(五)“计数排序” 与 “各排序实际用时测量”
c语言·算法·排序算法
Ralph_Y35 分钟前
C++:static
开发语言·c++