【leetcode】1207. Unique Number of Occurrences

题目如下:

Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.

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

解题思路:很简单的题目。

代码如下:

class Solution(object):
    def uniqueOccurrences(self, arr):
        """
        :type arr: List[int]
        :rtype: bool
        """
        dic = {}
        for i in arr:
            dic[i] = dic.setdefault(i,0) + 1
        dic_exist = {}
        for v in dic.itervalues():
            if v in dic_exist:
                return False
            dic_exist[v] = 1
        return True

原文地址:https://www.cnblogs.com/seyjs/p/11616700.html

时间: 2024-08-05 06:26:04

【leetcode】1207. Unique Number of Occurrences的相关文章

1207. Unique Number of Occurrences

Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique. Example 1: Input: arr = [1,2,2,1,1,3] Output: true Explanation: The value 1 has 3 occurrences, 2 has 2 a

【LeetCode】063. Unique Paths II

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the m

【LeetCode】009 Palindrome Number

题目:LeetCode 009 Palindrome Number 题意:判断一个整数是否为回文数,不要用额外空间 思路:我不会不用额外空间的方法,需要利用一个长度为20以内的字符串.将整数先写入一个字符串,然后判断首位字符是否相等即可. 代码如下: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 string s = to_string(x); 5 int len = s.size(); 6 for(int i = 0;

【Leetcode】Super Ugly Number

题目链接:https://leetcode.com/problems/super-ugly-number/ 题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13

【leetcode】1347. Minimum Number of Steps to Make Two Strings Anagram

题目如下: Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character. Return the minimum number of steps to make t an anagram of s. An Anagram of a string is a string that contains the same c

【leetcode】1395. Count Number of Teams

题目如下: There are n soldiers standing in a line. Each soldier is assigned a unique rating value. You have to form a team of 3 soldiers amongst them under the following rules: Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rat

【LeetCode】268. Missing Number

Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you i

【LeetCode】202. Happy Number

Happy Number Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the proc

【LeetCode】263. Ugly Number

Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note