Skip to content

Latest commit

 

History

History
66 lines (43 loc) · 1.14 KB

0~n-1中缺失的数字.md

File metadata and controls

66 lines (43 loc) · 1.14 KB

剑指 Offer 53 - II. 0~n-1中缺失的数字

https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/

题目描述

一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0~n-1之内。在范围0~n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字。

 

示例 1:

输入: [0,1,3]
输出: 2
示例 2:

输入: [0,1,2,3,4,5,6,7,9]
输出: 8
 

限制:

1 <= 数组长度 <= 10000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

按照题目的意思遍历

python代码

执行用时: 36 ms , 在所有 Python3 提交中击败了 94.56% 的用户 内存消耗: 15.5 MB , 在所有 Python3 提交中击败了 94.70% 的用户


class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        nums.insert(0,-1)
        for i in range(1,len(nums)):
            if(nums[i]!=nums[i-1]+1):
                return nums[i-1]+1
        return nums[-1]+1