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;
    }
};
相关推荐
北顾笙98019 小时前
day10-数据结构力扣
数据结构
2301_8042154119 小时前
模板元编程应用场景
开发语言·c++·算法
实心儿儿19 小时前
C++ —— 红黑树
java·开发语言·算法
炘爚19 小时前
C++(普通指针和成员的区别、指针的使用场景和存储内容)
数据结构·c++·算法
松☆19 小时前
C++ 控制台通讯录管理系统 —— 从零实现到完整解析(附可运行代码)
开发语言·网络·c++
Book思议-19 小时前
【数据结构考研真题】栈和队列(小题)
数据结构·考研·栈和队列·408小题
炘爚19 小时前
C++(在Mystring类中碰到的构造函数和析构函数以及深拷贝和浅拷贝的问题)
开发语言·c++·算法
hz_zhangrl20 小时前
CCF-GESP 等级考试 2026年3月认证C++四级真题解析
c++·程序设计·gesp·c++四级·gesp2026年3月·gesp c++四级
Fang fan20 小时前
Java集合
java·开发语言·算法