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;
    }
};
相关推荐
m0_706653236 分钟前
模板编译期排序算法
开发语言·c++·算法
历程里程碑7 分钟前
Linxu14 进程一
linux·c语言·开发语言·数据结构·c++·笔记·算法
木井巳11 分钟前
【递归算法】验证二叉搜索树
java·算法·leetcode·深度优先·剪枝
m0_5613596715 分钟前
嵌入式C++加密库
开发语言·c++·算法
近津薪荼15 分钟前
优选算法——双指针专题7(单调性)
c++·学习·算法
JiL 奥21 分钟前
Nexus制品归档(c/c++项目)
c语言·c++
j4455661123 分钟前
C++中的职责链模式实战
开发语言·c++·算法
m0_6860416127 分钟前
实时数据流处理
开发语言·c++·算法
波波侠834 分钟前
代码随想录算法训练营打卡第31天|56. 合并区间、738.单调递增的数字
算法
Snow_day.35 分钟前
有关线段树应用(1)
数据结构·算法·贪心算法·动态规划·图论