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;
    }
};
相关推荐
NAGNIP4 分钟前
一文搞懂深度学习中的池化!
算法·面试
山川行15 分钟前
关于《项目C语言》专栏的总结
c语言·开发语言·数据结构·vscode·python·算法·visual studio code
呜喵王阿尔萨斯17 分钟前
C and C++ code
c语言·开发语言·c++
旺仔.29118 分钟前
顺序容器:deque双端队列 详解
c++
Jasonakeke42 分钟前
我的编程来时路
java·c++·python
Yvonne爱编码1 小时前
Java 中的 hashCode () 与 equals () 核心原理、契约规范、重写实践与面试全解
java·开发语言·数据结构·python·hash
IT从业者张某某1 小时前
基于DEVC++实现一个控制台的赛车游戏-02-实现赛车游戏
开发语言·c++·游戏
yangSimaticTech1 小时前
整型数据的转换与比较并不简单
算法
老鼠只爱大米1 小时前
LeetCode经典算法面试题 #55:跳跃游戏(贪心法、动态规划、BFS等多种实现方案详解)
算法·leetcode·贪心算法·动态规划·bfs·java面试·跳跃游戏