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;
    }
};
相关推荐
lengxuenong4 分钟前
潍坊一中第四届编程挑战赛(初赛)题解
算法
wkd_0079 分钟前
【C++ | STL】std::vector 复制的几个方法总结
c++·stl·std vector·复制vector·vector复制
松涛和鸣11 分钟前
25、数据结构:树与二叉树的概念、特性及递归实现
linux·开发语言·网络·数据结构·算法
Han.miracle22 分钟前
数据结构--初始数据结构
算法·集合·大o表示法
List<String> error_P23 分钟前
C语言联合体:内存共享的妙用
算法·联合体
little~钰34 分钟前
可持久化线段树和标记永久化
算法
惺忪979840 分钟前
Qt C++11/14/17 新特性大全详解
开发语言·c++
Pacify_The_North44 分钟前
【C++11(二)】可变参数模板和 lambda表达式
java·开发语言·c++
顺顺 尼1 小时前
包装器c++11
开发语言·c++
獭.獭.1 小时前
C++ -- 二叉搜索树
数据结构·c++·算法·二叉搜索树