Simple Python 1 line Solution O(n)


  • 0
    D
    class Solution(object):
        def singleNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            return (3* sum(set(nums)) - sum(nums))/ (3*len(set(nums)) - len(nums))

  • 0
    A

    the question does not say the number show once or twice.
    If only show once, your solution is incorrect.


  • 0
    D

    Good point, actually, if only show once, above solution is correct, but if show twice, it is wrong.
    I have modified the solution.


  • -1
    A
    return (3* sum(set(nums)) - sum(nums))/2

  • 0
    D

    At first, I also used 2, but as ArkLiu commented, there is no given condition that the number show up twice. Strictly, it may show up any time bigger than 1.


Log in to reply
 

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