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;
    }
};
相关推荐
“αβ”2 分钟前
MySQL库的操作
linux·服务器·网络·数据库·c++·mysql·oracle
MobotStone18 分钟前
从问答到决策:Agentic AI如何重新定义AI智能体的未来
人工智能·算法
Shemol38 分钟前
二叉树的三种迭代遍历(无栈版本)-- 我在马克思主义课上的一些巧思
算法
胖咕噜的稞达鸭42 分钟前
进程状态,孤儿进程僵尸进程,Linux真实调度算法,进程切换
linux·运维·算法
月夜的风吹雨42 分钟前
【 C++哈希容器】:unordered_map与unordered_set深度解析
c++·哈希算法·unordered_map·unordered_set
RTC老炮1 小时前
webrtc降噪-WienerFilter源码分析与算法原理
算法·webrtc
hweiyu002 小时前
数据结构:数组
数据结构·算法
你的冰西瓜2 小时前
C++14 新特性详解:相较于 C++11 的主要改进
开发语言·c++·stl
无限进步_2 小时前
C语言单向链表实现详解:从基础操作到完整测试
c语言·开发语言·数据结构·c++·算法·链表·visual studio
初夏睡觉2 小时前
循环比赛日程表 题解
数据结构·c++·算法