Simple math problem

  • 0

    def findErrorNums(self, nums):
    n = len(nums)
    s = (1+n)*n/2
    sn = sum(nums)
    a = collections.Counter(nums).most_common(1)[0][0]
    b = s-sn+a
    return [a,b]
    let s = Sum(1...n) = (1+n)*n/2
    let sn=sum(nums) which means the current sum for the mismatch list,
    then s = sn -a + b which a is the duplicate number and b is the missing number.

Log in to reply

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