-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_0026.py
34 lines (22 loc) · 893 Bytes
/
problem_0026.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
"""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.
It doesn't matter what you leave beyond the returned length.
"""
class Solution:
def __init__(self):
self.nums = None
def removeDuplicates(self, nums, debug_mode=False) -> int:
len_nums = len(nums)
if len_nums < 2:
return len_nums
last_correct_idx = 0
for i in range(1, len_nums):
if nums[i] == nums[last_correct_idx]:
continue
nums[last_correct_idx + 1] = nums[i]
last_correct_idx += 1
if debug_mode: # Sanity check
self.nums = nums[: last_correct_idx + 1]
return last_correct_idx + 1