zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1207. 独一无二的出现次数 | Unique Number of Occurrences

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(let_us_code)
    ➤博主域名:https://www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/11566901.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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

    给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数。

    如果每个数的出现次数都是独一无二的,就返回 true;否则返回 false

    示例 1:

    输入:arr = [1,2,2,1,1,3]
    输出:true
    解释:在该数组中,1 出现了 3 次,2 出现了 2 次,3 只出现了 1 次。没有两个数的出现次数相同。

    示例 2:

    输入:arr = [1,2]
    输出:false
    

    示例 3:

    输入:arr = [-3,0,1,-3,1,1,1,-3,10,0]
    输出:true
    

    提示:

    • 1 <= arr.length <= 1000
    • -1000 <= arr[i] <= 1000

    Runtime: 12 ms
    Memory Usage: 20.7 MB
     1 class Solution {
     2     func uniqueOccurrences(_ arr: [Int]) -> Bool {
     3         var freq:[Int:Int] = [Int:Int]()
     4         for x in arr
     5         {
     6             freq[x,default:0] += 1
     7         }
     8         var occur:Set<Int> = Set<Int>()
     9         for (key,val) in freq
    10         {         
    11             if occur.contains(val)
    12             {
    13                 return false
    14             }
    15             occur.insert(val)
    16         }
    17         return true
    18     }
    19

     12ms

     1 class Solution {
     2     func uniqueOccurrences(_ arr: [Int]) -> Bool {
     3         var uniqueDic: [Int: Int] = [:]
     4       var occurArr = [Int]()
     5       for num in arr {
     6         if let occurNum = uniqueDic[num] {
     7           uniqueDic[num]! = occurNum + 1
     8         } else {
     9           uniqueDic[num] = 1
    10         }
    11       }
    12       for (_, value) in uniqueDic {
    13         if occurArr.contains(value) {
    14           return false
    15         }
    16         occurArr.append(value)
    17       }
    18       return true
    19     
    20     }
    21 }

    20ms
     1 class Solution {
     2     func uniqueOccurrences(_ arr: [Int]) -> Bool {
     3         var subArray = arr
     4         var count = arr.count
     5         var chkCount = count
     6         var boolArray = Array(repeating: false, count: count)
     7 
     8         for i in arr {
     9             subArray.removeAll() { $0 == i }
    10             count = subArray.count
    11 
    12             if chkCount > count {
    13                 if boolArray[chkCount - count] == true {
    14                     return false
    15                 }
    16                 boolArray[chkCount - count] = true
    17                 chkCount = count
    18             }
    19         }
    20         return true
    21     }
    22 }
  • 相关阅读:
    DataReader相关知识点⭐⭐⭐⭐⭐
    C# Distanct List集合
    RePlace函数
    DataTable和DataRow和DataColumn ⭐⭐⭐⭐⭐
    scrapy 基础组件专题(八):scrapy-redis 框架分析
    scrapy 基础组件专题(九):scrapy-redis 源码分析
    scrapy 基础组件专题(七):scrapy 调度器、调度器中间件、自定义调度器
    scrapy 基础组件专题(六):自定义命令
    scrapy 基础组件专题(五):自定义扩展
    scrapy 基础组件专题(四):信号运用
  • 原文地址:https://www.cnblogs.com/strengthen/p/11566901.html
Copyright © 2011-2022 走看看