-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path26.RemoveDuplicatesfromSortedArray.py
More file actions
147 lines (128 loc) · 4.67 KB
/
26.RemoveDuplicatesfromSortedArray.py
File metadata and controls
147 lines (128 loc) · 4.67 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Given a sorted array nums, remove the duplicates in-place such that each
element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by
modifying the input array in-place with O(1) extra memory.
Example:
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of
nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means
modification to the input array will be known to the caller as well.
"""
#---Solutin 1---
#Difficulty: Easy
#161 / 161 test cases passed.
#Runtime: 1480 ms
#Memory Usage: 15.5 MB
#Runtime: 1480 ms, faster than 5.04% of Python3 online submissions for Remove Duplicates from Sorted Array.
#Memory Usage: 15.5 MB, less than 5.74% of Python3 online submissions for Remove Duplicates from Sorted Array.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums: return 0
n = nums[-1]
for num in nums[-2::-1]:
if num == n:
nums.pop(nums.index(num))
else:
n = num
return len(nums)
#---End of Solution 1---
#---Solutin 2---
#Difficulty: Easy
#161 / 161 test cases passed.
#Runtime: 92 ms
#Memory Usage: 15.3 MB
#Runtime: 92 ms, faster than 43.66% of Python3 online submissions for Remove Duplicates from Sorted Array.
#Memory Usage: 15.3 MB, less than 5.74% of Python3 online submissions for Remove Duplicates from Sorted Array.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums: return 0
i = 0
l = len(nums)
n = nums[i]
indices = list()
for j in range(i+1, l):
if nums[j] == n:
indices.append(j)
n = nums[j]
for j in reversed(indices):
nums.pop(j)
return len(nums)
#---End of Solution 2---
#---Solution 3---
#Difficulty: Easy
#161 / 161 test cases passed.
#Runtime: 88 ms
#Memory Usage: 15.7 MB
#Runtime: 88 ms, faster than 57.80% of Python3 online submissions for Remove Duplicates from Sorted Array.
#Memory Usage: 15.7 MB, less than 5.74% of Python3 online submissions for Remove Duplicates from Sorted Array.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums: return 0
n = nums[0]
i = list()
for j in range(1, len(nums)):
if nums[j] == n:
i.append(j)
n = nums[j]
i.reverse()
for j in i:
nums.pop(j)
return len(nums)
#---End of Solution 3---
#---Solution 4---
#Difficulty: Easy
#161 / 161 test cases passed.
#Runtime: 84 ms
#Memory Usage: 15.6 MB
#Runtime: 84 ms, faster than 76.46% of Python3 online submissions for Remove Duplicates from Sorted Array.
#Memory Usage: 15.6 MB, less than 5.74% of Python3 online submissions for Remove Duplicates from Sorted Array.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums: return 0
i = 0
n = nums[i]
for j in range(i+1, len(nums)):
if n != nums[j]:
nums[i+1] = nums[j]
i += 1
n = nums[j]
nums = nums[:i+1]
return len(nums)
#---End of Solution 4---
#---Solution 4.1---
#Difficulty: Easy
#161 / 161 test cases passed.
#Runtime: 76 ms
#Memory Usage: 15.6 MB
#Runtime: 76 ms, faster than 96.62% of Python3 online submissions for Remove Duplicates from Sorted Array.
#Memory Usage: 15.6 MB, less than 5.74% of Python3 online submissions for Remove Duplicates from Sorted Array.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums: return 0
i = 0
n = nums[i]
for j in range(i+1, len(nums)):
if n != nums[j]:
nums[i+1] = nums[j]
i += 1
n = nums[j]
nums = nums[:i+1]
return i+1
#---End of Solution 4.1---
#---Solution 5---
#Difficulty: Easy
#161 / 161 test cases passed.
#Runtime: 84 ms
#Memory Usage: 15.5 MB
#Runtime: 84 ms, faster than 76.46% of Python3 online submissions for Remove Duplicates from Sorted Array.
#Memory Usage: 15.5 MB, less than 5.74% of Python3 online submissions for Remove Duplicates from Sorted Array.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
nums[:] = sorted(set(nums))
return len(nums)
#---End of Solution 5---