136. Single Number in Python (one line code)


  • 0
    L

    Haven't tried the other language, but there is very neat solution of python, with one line of code only.

    The idea is to use set() to find the unique numbers, then make a sum and times 2. Finally, substract the sum of the original list. Every integer exists 2 times except one. So the answer is quite obvious.

    class Solution(object):
        def singleNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            return 2*sum(set(nums)) - sum(nums)
    

  • 0
    L

    @1337c0d3r This might be my first activity in leetcode community, I may propose a stupid solution, but seriously?? -1 reputation?? hurt so much!!!


Log in to reply
 

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