题目
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
解题思路
求出1-n的和,减去数组的总和
代码
class Solution:
def missingNumber(self, nums: List[int]) -> int:
return (len(nums) + 1) * len(nums) // 2 - sum(nums)