Ruka
1 min readJul 28, 2020

[LeetCode][python3]0026. Remove Duplicates from Sorted Array

Start the journey
N2I -2020.03.19

  1. My first solution
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
pre=nums[0]
pre_index=1
for i in nums[1:]:
if i!=pre:
nums[pre_index]=i
pre=i
pre_index+=1
#print(nums)
return pre_index
N2I -2020.03.19

Note:

Explanation:

Notice that we have to follow the condition in the question: “Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.”

Ruka
Ruka

Written by Ruka

HI I’m Ruka. Now a SWE in Taiwan. Contact via email: nayzi9999@gmail.com

No responses yet