Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 726 Bytes

0268. Missing Number.md

File metadata and controls

34 lines (25 loc) · 726 Bytes

题目

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

Example 1:

Input: [3,0,1]
Output: 2

Example 2:

Input: [9,6,4,2,3,5,7,0,1]
Output: 8

我的思路

首先,从数组长度可以判断出来一共应该有多少个数字。然后将数组中的数存入字典,再进行比较找出缺失的那个数字。

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        total = len(nums) + 1
        dic = {}
        for x in nums:
            dic[x] = x
        for i in range(total):
            if i not in dic.keys():
                return i