After reading from comments I realized the question don't want us to remove elements from the list, but to put non-duplicated list in the beginning and return the size of it. And OJ doesn't care about the rest of the list.
I don't understand why this question don't allow us to use del to remove element from the list. Maybe because of the definition of Array structure in C?
class Solution: # @param a list of integers # @return an integer def removeDuplicates(self, A): if not A: return 0 end = len(A) read = 1 write = 1 while read < end: if A[read] != A[read-1]: A[write] = A[read] write += 1 read += 1 return write
At first, you said that the question want us to put non-duplicated list in the beginning and return the size of it. I have the same finding with you.But I don't know why the question don't care about the array.
The question said:
Given input array A = [1,1,2],Your function should return length = 2, and A is now [1,2].
I use 'del' in my code, and it was accepted.But it runs 136ms,too slow.Thatis my code:
def removeDuplicates(self, nums):
i,len_s = 0,len(nums)
len_s -= 1
i += 1
@the_important I think the nums list will be tested as well
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.