Skip to content

Commit 9af32cd

Browse files
committed
chore: add daily leetcode post 80_translated
1 parent 4d230c5 commit 9af32cd

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: Python beat98.40% collectionsofCounter method!
3+
date: '2024.01.01 0:00'
4+
tags:
5+
- - Python
6+
- - solved
7+
- answer
8+
abbrlink: 73b5ce9c
9+
category:
10+
---
11+
12+
13+
14+
15+
16+
17+
Pyhton `collection` In the bag `Counter()` Be rightlist里出现of元素conduct计数,And the output is a dictionary。
18+
for example`nums=[1, 1, 1, 2, 2, 3]` ToCounter后of结果是`Counter({1: 3, 2: 2, 3: 1})`
19+
1. So traverse a dictionary,when`value>3`of时候`value=2`,Can be greater than2indivualof元素计数become2indivual。
20+
1. So we will`Counter({1: 3, 2: 2, 3: 1})`become`Counter({1: 2, 2: 2, 3: 1})`后再Toelementsconductlist操作就可以得到改变后of列表了。
21+
22+
---
23+
24+
Due to the meaning of the question“Input the array「Quote」方式传递of”,So we willnumsJust fill in and fill in
25+
26+
27+
```python
28+
from collections import Counter # Imported package
29+
class Solution:
30+
def removeDuplicates(self, nums: List[int]) -> List[int]:
31+
dict1 = Counter(nums)
32+
for i in dict1:
33+
if dict1[i] > 2:
34+
dict1[i] = 2
35+
list1 = list(dict1.elements())
36+
nums.clear() # clear the list
37+
nums.extend(list1) # Add the collection to the list
38+
return len(nums)
39+
```
40+
Complexity analysis
41+
42+
time complexity:`O(n)`,in `n` 是数组of长度。We have a maximum of this array once。
43+
44+
Spatial complexity:`O(1)`。我们只需要常数of空间存储若干变量。

0 commit comments

Comments
 (0)