Posted on 2022-11-30 13:54
Uriel 閱讀(40)
評論(0) 編輯 收藏 引用 所屬分類:
閑來無事重切Leet Code 、
大水題
給定一列數(shù),問其中每個數(shù)字出現(xiàn)的次數(shù)是否是unique的,先用一個dict記錄每個數(shù)出現(xiàn)次數(shù),再用一個set判斷出現(xiàn)次數(shù)是否兩兩不同
1 #1207
2 #Runtime: 49 ms
3 #Memory Usage: 13.5 MB
4
5 class Solution(object):
6 def uniqueOccurrences(self, arr):
7 """
8 :type arr: List[int]
9 :rtype: bool
10 """
11 dct = {}
12 for i in arr:
13 if i not in dct:
14 dct[i] = 1
15 else:
16 dct[i] += 1
17 occ = set()
18 for i in dct:
19 if dct[i] not in occ:
20 occ.add(dct[i])
21 else:
22 return False
23 return True