LeetCode--Python解析【Missing Number】(268)

题目:

方法:

class Solution:
    def missingNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        a = len(nums)
        b = int(a*(a+1)/2)
        for n in nums:
            b -= n
        return b

首先用等差数列求和公式,求出前n项和。

然后以此减去list中的元素

最后的差值就是没有出现过的元素。

猜你喜欢

转载自blog.csdn.net/ZJRN1027/article/details/81171157