题目描述:
一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0~n-1之内。在范围0~n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字。
链接:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof
输入: [0,1,3] 输出: 2
题解:使用循环的方式查找是否缺少0~n-1之间的值,如果有则返回,否则既缺少最后的n-1值。
class Solution(object):
def missingNumber(self, nums):
for i in range(len(nums)):
if i not in nums:
return i
return len(nums)
if __name__ == "__main__":
soul=Solution()
count=soul.missingNumber([0,1,3])
print(count)