-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_1.py
More file actions
55 lines (45 loc) · 1.33 KB
/
question_1.py
File metadata and controls
55 lines (45 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# @Time : 2018/4/7 下午9:30
# @Author : cancan
# @File : question_1.py
# @Function : 从排序数组中删除重复项
"""
Question:
给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度。
不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点。
Example:
给定数组: nums = [1,1,2],
你的函数应该返回新长度 2, 并且原数组nums的前两个元素必须是1和2
不需要理会新的数组长度后面的元素
"""
class Solution1:
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a = []
d = []
for i, v in enumerate(nums):
if v not in a:
a.append(v)
else:
d.append(i)
for _ in sorted(d, reverse=True):
del nums[_]
return len(nums)
class Solution2:
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a = nums.copy()
nums.clear()
nums.extend(sorted(list(set(a)), reverse=False))
return len(nums)
if __name__ == '__main__':
a = [-1, 0, 0, 3, 3]
s = Solution2()
print(s.removeDuplicates(a))
print(a)