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;
    }
};
相关推荐
qq_29655327几秒前
【LeetCode】最大子数组乘积:三种解法从暴力到最优
数据结构·算法·leetcode·职场和发展·动态规划·柔性数组
不知名的老吴1 分钟前
关于C++中的placement new
数据结构·c++·算法
平行侠1 分钟前
023Pollard-ρ 因子分解算法
数据结构·算法
tankeven5 分钟前
C++ 封装
c++
谭欣辰6 分钟前
C++倍增算法详解
数据结构·c++·算法
MATLAB代码顾问6 分钟前
差分进化算法(DE)原理与Python实现
开发语言·python·算法
MicroTech20258 分钟前
微算法科技(NASDAQ :MLGO)基于后量子密码学的动态BFT共识机制:QDBFT架构
科技·算法·密码学
Brilliantwxx15 分钟前
【C++】认识 list(初步认识+模拟实现)
开发语言·数据结构·c++·笔记·算法·list
南宫萧幕18 分钟前
锂电池二阶 RC 模型仿真实战:从理论解析到 Simulink 闭环搭建全流程
开发语言·人工智能·算法·机器学习
Hical_W18 分钟前
Hical 踩坑实录五部曲(一):Boost.Asio 协程开发的 N 个坑
网络·c++·开源