日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode781. 森林中的兔子 | Rabbits in Forest

發布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode781. 森林中的兔子 | Rabbits in Forest 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:?https://www.cnblogs.com/strengthen/p/10543038.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how many other rabbits have the same color as them. Those?answers?are placed in an array.

Return the minimum number of rabbits that could be in the forest.

Examples: Input: answers = [1, 1, 2] Output: 5 Explanation: The two rabbits that answered "1" could both be the same color, say red. The rabbit than answered "2" can't be red or the answers would be inconsistent. Say the rabbit that answered "2" was blue. Then there should be 2 other blue rabbits in the forest that didn't answer into the array. The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.Input: answers = [10, 10, 10] Output: 11Input: answers = [] Output: 0

Note:

  • answers?will have length at most?1000.
  • Each?answers[i]?will be an integer in the range?[0, 999].

  • 森林中,每個兔子都有顏色。其中一些兔子(可能是全部)告訴你還有多少其他的兔子和自己有相同的顏色。我們將這些回答放在?answers?數組里。

    返回森林中兔子的最少數量。

    示例: 輸入: answers = [1, 1, 2] 輸出: 5 解釋: 兩只回答了 "1" 的兔子可能有相同的顏色,設為紅色。 之后回答了 "2" 的兔子不會是紅色,否則他們的回答會相互矛盾。 設回答了 "2" 的兔子為藍色。 此外,森林中還應有另外 2 只藍色兔子的回答沒有包含在數組中。 因此森林中兔子的最少數量是 5: 3 只回答的和 2 只沒有回答的。輸入: answers = [10, 10, 10] 輸出: 11輸入: answers = [] 輸出: 0

    說明:

  • answers?的長度最大為1000。
  • answers[i]?是在?[0, 999]?范圍內的整數。

  • Runtime:?20 ms Memory Usage:?19.3 MB 1 class Solution { 2 func numRabbits(_ answers: [Int]) -> Int { 3 var res:Int = 0 4 var m:[Int:Int] = [Int:Int]() 5 for ans in answers 6 { 7 m[ans,default:0] += 1 8 } 9 for (key,val) in m 10 { 11 res += (val + key) / (key + 1) * (key + 1) 12 } 13 return res 14 } 15 }

    24ms

    1 class Solution { 2 func numRabbits(_ answers: [Int]) -> Int { 3 var counter = [Int: Int]() 4 answers.forEach{ counter[$0, default: 0] += 1 } 5 6 var ret = 0 7 for (n, c) in counter { 8 let colors = (c + n) / (n + 1) 9 ret += colors * (n + 1) 10 } 11 return ret 12 } 13 }

    24ms

    1 class Solution { 2 func numRabbits(_ answers: [Int]) -> Int { 3 if answers.count == 0 { 4 return 0 5 } 6 7 let sortAns = answers.sorted() 8 var first = -1 9 10 var total = 0 11 var sep = 0 12 13 for (_, itemCount) in sortAns.enumerated() { 14 if itemCount != first || sep == first { 15 first = itemCount 16 sep = 0 17 18 total += 1 + itemCount 19 } else { 20 sep += 1 21 } 22 } 23 return total 24 } 25 }

    48ms

    1 class Solution { 2 func numRabbits(_ answers: [Int]) -> Int { 3 var mark = [Int : Int]() 4 for answer in answers { 5 if let count = mark[answer] { 6 mark[answer] = count + 1 7 } else { 8 mark[answer] = 1 9 } 10 } 11 12 var result = 0 13 for (key, value) in mark { 14 let groupNum = key + 1 15 var group = value / groupNum 16 if value % groupNum != 0 { 17 group += 1 18 } 19 result += group * groupNum 20 } 21 return result 22 } 23 }

    ?

    轉載于:https://www.cnblogs.com/strengthen/p/10543038.html

    總結

    以上是生活随笔為你收集整理的[Swift]LeetCode781. 森林中的兔子 | Rabbits in Forest的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。