-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path80removeDuplicate.py
49 lines (40 loc) · 1.25 KB
/
80removeDuplicate.py
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
import json
class Solution:
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l = len(nums)
if l <= 2:
return l # 因为每个元素最多出现两次,所以头两个元素一定符合要求
index = 2
for i in range(2, l):
if nums[i] != nums[index - 2]: # 我们要阻止三个连续相同的元素
nums[index] = nums[i]
index = index + 1
return index
def stringToIntegerList(input):
return json.loads(input)
def integerListToString(nums, len_of_list=None):
if not len_of_list:
len_of_list = len(nums)
return json.dumps(nums[:len_of_list])
def main():
import sys
import io
def readlines():
for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
yield line.strip('\n')
lines = readlines()
while True:
try:
line = next(lines)
nums = stringToIntegerList(line)
ret = Solution().removeDuplicates(nums)
out = integerListToString(nums, len_of_list=ret)
print(out)
except StopIteration:
break
if __name__ == '__main__':
main()