Problem
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.文章来源:https://www.toymoban.com/news/detail-626162.html
Algorithm
Sum all the numbers as x x x and use n ( n + 1 ) 2 − x \frac{n(n+1)}{2} - x 2n(n+1)−x.文章来源地址https://www.toymoban.com/news/detail-626162.html
Code
class Solution:
def missingNumber(self, nums: List[int]) -> int:
sum_, n_ = 0, len(nums)
for num in nums:
sum_ += num
return n_ * (n_ + 1) // 2 - sum_
到了这里,关于Leetcode 268. Missing Number的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!