Skip to content

Commit 91014d6

Browse files
committed
Here's a O(n^2) solution
1 parent 034fbc1 commit 91014d6

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

80_remove_duplicate_from_sorted_array_2.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,28 @@ def removeDuplicates(nums):
1616
j += 1
1717
return i
1818

19-
# n = [1,1,1,2,2,3]
20-
n = [0,0,1,1,1,1,2,3,3]
21-
print removeDuplicates(n)
19+
def removeDuplicates2(nums):
20+
i, j, c, r = 0, 0, 0, 0
21+
while j < len(nums):
22+
# print i,j,c,r,nums
23+
if nums[i] == nums[j]:
24+
c += 1
25+
j += 1
26+
continue
27+
assert c > 0
28+
if c == 1:
29+
i = j
30+
r += 1
31+
else:
32+
i += 2
33+
nums[i:] = nums[j:]
34+
j = i
35+
r += 2
36+
c = 0
37+
r += min(c, 2)
38+
return r
39+
40+
n = [1,1,1]
41+
# n = [0,0,1,1,1,1,2,3,3]
42+
print removeDuplicates2(n)
2243
print n

0 commit comments

Comments
 (0)