Pyhton collection
In the bag Counter()
Be rightlist里出现of元素conduct计数,And the output is a dictionary。
for examplenums=[1, 1, 1, 2, 2, 3]
ToCounter后of结果是Counter({1: 3, 2: 2, 3: 1})
。
- So traverse a dictionary,when
value>3
of时候value=2
,Can be greater than2indivualof元素计数become2indivual。 - So we will
Counter({1: 3, 2: 2, 3: 1})
becomeCounter({1: 2, 2: 2, 3: 1})
后再Toelementsconductlist操作就可以得到改变后of列表了。
Due to the meaning of the question“Input the array「Quote」方式传递of”,So we willnumsJust fill in and fill in
1 | from collections import Counter # Imported package |
Complexity analysis
time complexity:O(n)
,in n
是数组of长度。We have a maximum of this array once。
Spatial complexity:O(1)
。我们只需要常数of空间存储若干变量。