Re: [閒聊] 每日LeetCode已回收
看板Marginalman作者sustainer123 (caster )時間1年前 (2024/01/17 18:30)推噓0(0推 0噓 0→)留言0則, 0人參與討論串610/719 (看更多)
※ 引述《JerryChungYC (JerryChung)》之銘言:
: ※ 引述《yam276 (史萊哲林的優等生)》之銘言:
: : https://leetcode.com/problems/unique-number-of-occurrences/
: : 1207. Unique Number of Occurrences
: : 給一個陣列,判斷每種數字的出現次數是否為唯一
: 思路:
: Counter回傳由List內項目與出現次數組成的dict
: 再用set判斷每個出現次數是否只出現一次
: Python3 code:
: -------------------------------------------------------------
: class Solution:
: def uniqueOccurrences(self, arr: List[int]) -> bool:
: values = Counter(arr).values()
: return len(values) == len(set(values))
: -------------------------------------------------------------
: 話說通常會找其他解法嗎
: 還是解過就關了
: 昨天的建class第一次遇到 忘了解 又斷紀錄了 :(
Python3 code:
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
has = {}
for e in arr:
if e in has:
has[e] += 1
else:
has[e] = 1
return len(has) == len(set([times for times in has.values()]))
想法差不多
不過我不太熟counter
有空研究一下
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.43.158.6 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1705487437.A.443.html
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 610 之 719 篇):