Skip to main content

LeetCode 26

26. Remove Duplicates from Sorted Array​

Solution​

class Solution:
def removeDuplicates(self, nums: List[int]) -> int:

# Pointer to track the position of the unique elements
i = 0

# Iterate through the array starting from the second element
for j in range(1, len(nums)):
# If current element is different from the last unique element
if nums[j] != nums[i]:
# Move the pointer i forward and replace the next position with nums[j]
i += 1
nums[i] = nums[j]

# Return the length of the array with unique elements
return i + 1

Time Complexity​

λ°°μ—΄μ˜ 길이만큼 λ°˜λ³΅ν•˜λ―€λ‘œ O(n)O(n)이닀.

Space Complexity​

in-place둜 μž‘μ—…μ„ μˆ˜ν–‰ν•˜λ―€λ‘œ μΆ”κ°€ 곡간이 ν•„μš”ν•˜μ§€ μ•Šλ‹€. λ”°λΌμ„œ 곡간 λ³΅μž‘λ„λŠ” O(1)O(1)이닀.

Comment​

두 배열이 μ •λ ¬λ˜μ–΄