[HELP] Python code, wrong answer.


  • 0
    X

    Anyone can help me with my code?

    I don't know what's wrong with it.

    class Solution(object):
        def removeDuplicates(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            result = []
            i = 0
            for ele in nums:
                try:
                    if ele == result[-1] and i == 1:
                        result.append(ele)
                        i = 2
                    elif ele == result[-1] and i == 2:
                        continue
                    else:
                        result.append(ele)
                        i = 1
                except:
                    result.append(ele)
                    i = 1
            return len(result)
    

  • 0
    S

    you also need to modify the original list with the duplicates removed (of course only remove those appeared more than twice)


  • 0
    X

    So my code will return the correct length but did not remove the duplicate elements in the original list, right?

    Thank you for the answer!


Log in to reply
 

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