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;
    }
};
相关推荐
沐怡旸6 分钟前
【穿越Effective C++】条款22:将成员变量声明为private——封装的边界与设计的自由
c++
强化学习与机器人控制仿真33 分钟前
字节最新开源模型 DA3(Depth Anything 3)使用教程(一)从任意视角恢复视觉空间
人工智能·深度学习·神经网络·opencv·算法·目标检测·计算机视觉
Teacher.chenchong1 小时前
R语言实现物种分布预测与生态位分析:多元算法实现物种气候生态位动态分析与分布预测,涵盖数据清洗、模型评价到论文写作全流程
开发语言·算法·r语言
mit6.8241 小时前
高维状态机dp|环形dp
算法
Swift社区1 小时前
LeetCode 427 - 建立四叉树
算法·leetcode·职场和发展
u***j3241 小时前
算法设计模式总结
算法·设计模式
vir021 小时前
交换瓶子(贪心)
数据结构·算法
G***66912 小时前
算法设计模式:贪心与动态规划
算法·设计模式·动态规划
墨染点香2 小时前
LeetCode 刷题【160. 相交链表】
算法·leetcode·链表
少睡点觉2 小时前
LeetCode 238. 除自身以外数组的乘积 问题分析+解析
java·算法·leetcode