Accepted python solution


  • 4
    M

    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

  • -1
    K
    This post is deleted!

  • 1
    T

    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].
    Thanks


  • -1
    E

    I use 'del' in my code, and it was accepted.But it runs 136ms,too slow.Thatis my code:
    class Solution(object):
    def removeDuplicates(self, nums):
    i,len_s = 0,len(nums)
    while i<len(nums)-1:
    if nums[i]==nums[i+1]:
    del nums[i]
    len_s -= 1
    else:
    i += 1
    return len(nums)


  • 0
    S

    @the_important I think the nums list will be tested as well


  • 0
    G
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.