原題
給出一個(gè)包含 0 .. N 中 N 個(gè)數(shù)的序列转捕,找出0 .. N 中沒有出現(xiàn)在序列中的那個(gè)數(shù)灸撰。
樣例
N = 4 且序列為 [0, 1, 3] 時(shí)哨啃,缺失的數(shù)為2号阿。
解題思路
- 通過數(shù)學(xué)公式計(jì)算所有數(shù)的和迁沫,然后減掉已知數(shù)組中的的數(shù)
完整代碼
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return None
total = (1 + len(nums)) * len(nums) / 2
for num in nums:
total -= num
return total