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;
    }
};
相关推荐
XZHOUMIN几秒前
【生成pdf格式的报告】
c++·pdf·mfc
初生牛犊不怕苦5 分钟前
与AI一起学习《C专家编程》:数组与指针
c语言·学习·算法
elseif12317 分钟前
浅谈 C++ 学习
开发语言·c++·学习
Kk.080228 分钟前
数据结构|排序算法(二) 冒泡排序
数据结构·算法·排序算法
沛沛rh4532 分钟前
深入并发编程:从 C++ 到 Rust 的学习笔记
c++·笔记·学习·算法·rust
小CC吃豆子1 小时前
C/C++中 int 的最大最小值
c语言·开发语言·c++
欧米欧1 小时前
C++模板初阶
开发语言·c++
Kk.08021 小时前
数据结构|排序算法(二) 希尔排序
数据结构·算法·排序算法
CheerWWW1 小时前
C++学习笔记——初始化列表、创建和实例化对象、new 关键字、隐式构造与 explicit 关键字、运算符与运算符重载
c++·笔记·学习