Python "Wrong Answer", leetcode is wrong?

  • 0

    Hi all,

    I have the code for this question, pretty straightforward:

    class Solution:
        # @param {integer[]} nums
        # @return {integer}
        def removeDuplicates(self, nums):
            self.nums = nums
            s = len(nums)
            if s == 0 or s == 1:
                return s
            k = nums[0]
            for i in nums[1:]:
                if i-k == 0:
                    s = s-1
                    k = i
            return s

    Based on the problem, the required output should be the length with no repeated numbers.

    But I have this error:

    Input: [1,1,2] Output: [1,1] Expected: [1,2]

    actually, I tested this on Pycharm, the output is 2...

    Leetcode has system error?


  • 2

    The "Output" is of course the combination of the return value and the changed list. And you're btw not changing the list in the slightest.

  • 0
    This post is deleted!

  • 0

    You should change the input list ("nums" in this problem).

    My answer:

    def removeDuplicates(self, nums):
            n = len(nums)
            if n <= 1:
                return n
            for i in xrange(n):
                while i < n - 1 and nums[i] == nums[i+1] :
                    n = n - 1
            return n

  • 0

    I would not accept your solution as valid to be honest. You're making python do all the work for you.

Log in to reply

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