Leetcode - Elementary Array

1. Remove duplicates from sorted array

Given a sorted array, you need to remove duplicate elements in place so that each element occurs only once, and return the new length of the removed array.

Instead of using extra array space, you have to modify the input array in-place and do it with O(1) extra space.

1 class Solution:
2     def removeDuplicates(self,nums):
3         i =0    
4         while i < len(nums)-1:
5             if nums[i+1] == nums[i]:
6                 nums.remove(nums[i+1])
7             i +=1
8         return len(nums)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325903471&siteId=291194637