leetcode 268. Missing Number 缺失数字 python 多种思路,一行代码

class Solution:
    def missingNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """

        # method two   遍历速度太慢
        # return  [i for i in range(len(nums)+1) if i not in nums][0]

        # method three   集合操作
        # return  list(set(list(range(len(nums)+1))) - set(nums))[0]  


        # method one 从数学角度考虑
        return int(len(nums)*(len(nums)+1)/2 - sum(nums)) 


猜你喜欢

转载自blog.csdn.net/huhehaotechangsha/article/details/80825787