Why my python solution is wrong? using set(),I've tested it in my computer


  • 0
    C
    def removeDuplicates(self, nums):
        nums = list(set(nums))
        return len(nums)
    

    when nums = [1,1,2] after nums = list(set(nums)), nums is [1,2] on my computer, but it actually is [1,1] on OJ?
    what's wrong?


  • 3

    The reason is nums = list(set(nums)) just generate a new list, it doesn't change the orignal nums。

    def removeDuplicates(nums):
        nums = list(set(nums))
        print nums
        return len(nums)
    if __name__ == "__main__":
        nums = [1, 1, 2]
        print nums
        n = removeDuplicates(nums)
        print nums
        print nums[:n]
    

Log in to reply
 

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