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β
λ°°μ΄μ κΈΈμ΄λ§νΌ λ°λ³΅νλ―λ‘ μ΄λ€.
Space Complexityβ
in-placeλ‘ μμ μ μννλ―λ‘ μΆκ° 곡κ°μ΄ νμνμ§ μλ€. λ°λΌμ κ³΅κ° λ³΅μ‘λλ μ΄λ€.
Commentβ
λ λ°°μ΄μ΄ μ λ ¬λμ΄