Python one line solution

  • 0

    The trick is we analytically know the sum of the array does not have any incorrect data: len(nums)*(len(nums)+1)/2
    On the other hand, transforming the array to a set and sum it up, we will know the sum missing a number, then we get the answer quite straightforward through two subtractions.

    class Solution(object):
        def findErrorNums(self, nums):
            :type nums: List[int]
            :rtype: List[int]
            return [sum(nums)-sum(set(nums)),(len(nums)+1)*len(nums)/2-sum(set(nums))]

Log in to reply

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