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;
    }
};
相关推荐
阿猿收手吧!1 分钟前
【C++】Ranges 工厂视图与投影机制
开发语言·c++
.小墨迹2 分钟前
局部规划中的TEB,DWA,EGOplanner等算法在自动驾驶中应用?
开发语言·c++·人工智能·学习·算法·机器学习·自动驾驶
AI科技星6 分钟前
张祥前统一场论 22 个核心公式及常数
服务器·人工智能·线性代数·算法·矩阵·概率论
苏婳6667 分钟前
阿里巴巴校招软件笔试题经典
算法
你的冰西瓜12 分钟前
C++中的priority_queue容器详解
开发语言·c++·stl
阿猿收手吧!13 分钟前
【数据结构】高效掌握并查集:核心原理与实战
数据结构·算法
励ℳ16 分钟前
机器学习之线性回归算法:从原理到实践的全面解析
算法·机器学习·线性回归
_Twink1e17 分钟前
[算法教学]一、前置知识
算法
柒儿吖19 分钟前
三方库 Emoji Segmenter 在 OpenHarmony 的 lycium 适配与测试
c++·c#·openharmony
冬风诉22 分钟前
cuda核函数
c++·cuda