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
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
缺失数字
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
示例 1:
输入: [3,0,1] 输出: 2
示例 2:
输入: [9,6,4,2,3,5,7,0,1] 输出: 8
说明:
你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现?
本质就是求差值, 完整列表元素之和与缺失元素列表之和的差值,就是所求的缺失元素.
class Solution: @classmethod def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ return (int(len(nums) * (len(nums) + 1) / 2) - sum(nums))
浙公网安备 33010602011771号