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;
    }
};
相关推荐
XiYang-DING3 小时前
【LeetCode】 225.用队列实现栈
算法·leetcode·职场和发展
ybzj.3 小时前
2025年第十六届蓝桥杯省赛C/C++大学B组 个人题解
c++
花月C3 小时前
线性动态规划(Linear DP)
算法·动态规划·代理模式
派大星~课堂3 小时前
【力扣-148. 排序链表】Python笔记
python·leetcode·链表
量子炒饭大师4 小时前
【C++ 11】Cyber骇客 最后的一片净土 ——【C++11的 简单介绍 + 发展历史】历史唯物主义者带你理顺C++发展的由来
c++·dubbo·c++11
hetao17338374 小时前
2025-03-24~04-06 hetao1733837 的刷题记录
c++·算法
小白菜又菜4 小时前
Leetcode 657. Robot Return to Origin
python·leetcode·职场和发展
_深海凉_4 小时前
LeetCode热题100-环形链表
算法·leetcode·链表
原来是猿4 小时前
Linux进程信号详解(三):信号保存
开发语言·c++·算法
2401_892070984 小时前
算法与数据结构精讲:最大子段和(暴力 / 优化 / 分治)+ 线段树从入门到实战
c++·算法·线段树·最大子段和