【LeetCode】面试题 17.19. 消失的两个数字

题目

面试题 17.19. 消失的两个数字

给定一个数组,包含从 1 到 N 所有的整数,但其中缺了两个数字。你能在 O(N) 时间内只用 O(1) 的空间找到它们吗?

以任意顺序返回这两个数字均可。

示例 1:

输入: [1]
输出: [2,3]

示例 2:

输入: [2,3]
输出: [1,4]

提示:

  • nums.length <= 30000

题解1

思路

  • 将数组排序,若 n u m s [ i ] − n u m s [ i − 1 ] ≠ 1 nums[i]-nums[i-1] \ne 1 nums[i]nums[i1]=1则说明 n u m s [ i ] − 1 nums[i]-1 nums[i]1不在数组中
  • 同时将消失的数字在数组首和数组尾两种情况单独考虑

代码

class Solution:
    def missingTwo(self, nums: List[int]) -> List[int]:
        if not nums: return [1,2]
        elif len(nums) == 1:
            if nums[0] == 1: return [2,3]
            elif nums[0] == 2: return [1,3]
            elif nums[0] == 3: return [1,2]
            else: assert False
        nums.sort()
        ret = []
        if nums[0] > 2: return [1,2]
        elif nums[0] == 2: ret.append(1)
        i = 1
        while i < len(nums):
            if nums[i] - nums[i-1] > 1:
                ret.append(nums[i] - 1)
            i+=1
        if len(ret) == 0:
            ret.append(nums[-1] + 1)
            ret.append(nums[-1] + 2)
        elif len(ret) == 1:
            ret.append(nums[-1] + 1)
        return ret

复杂度

  • 时间复杂度: O ( n log ⁡ n ) O(n\log n) O(nlogn)
  • 空间复杂度: O ( log ⁡ n ) O(\log n) O(logn)

题解2

思路

  • 数组的长度为 n n n则说明 [ 1 , n + 2 ] [1,n+2] [1,n+2]区间内,不曾出现过的数则说明消失了

代码

class Solution:
    def missingTwo(self, nums: List[int]) -> List[int]:
        visited = [False] * (len(nums)+2)
        for n in nums:
            visited[n-1] = True
        ret = []
        for i in range(len(visited)):
            if not visited[i]: ret.append(i+1)
        return ret

复杂度

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( n ) O(n) O(n)

题解3

思路

  • 假设数组消失的数为 x 1 , x 2 x_1, x_2 x1,x2,则 x 1 + x 2 = ∑ n u m − ∑ ( r a n g e ( 1 , n + 2 ) ) x_1+x_2=\sum num - \sum(range(1, n+2)) x1+x2=num(range(1,n+2))
  • 若输入为哈希表,则可以通过 x 1 ∉ n u m s ∧ x 2 ∉ n u m s x_1 \notin nums \land x_2\notin nums x1/numsx2/nums来判断 可惜不是╮(╯▽╰)╭

代码

class Solution:
    def missingTwo(self, nums: List[int]) -> List[int]:
        s = set(nums)
        n = len(s)
        currentSum = sum(s)
        expectSum = (n+2)*(n+3)//2
        diff = expectSum - currentSum
        for i in range(1, n+3):
            if i not in s and (diff - i) not in s and (diff-i)<n + 3: return [i, diff-i]

复杂度

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( n ) O(n) O(n)

题解4

思路

  • 继题解3,考虑构造线性无关方程组,考虑:
    { a = x 1 + x 2 = ( n + 2 ) ( n + 3 ) 2 − ∑ ( n u m s ) b = x 1 2 + x 2 2 = ( n + 2 ) ( n + 3 ) ( 2 n + 5 ) 6 − ∑ n   ∈ n u m s ( n 2 ) \left\{\begin{aligned} & a = x_1+x_2 = \frac{(n+2)(n+3)}{2} - \sum(nums)\\ & b = x_1^2 + x_2^2 = \frac{(n+2)(n+3)(2n+5)}{6} -\sum_{n \ \in nums}(n^2)\\ \end{aligned}\right. a=x1+x2=2(n+2)(n+3)(nums)b=x12+x22=6(n+2)(n+3)(2n+5)n nums(n2)

  • 可以求解
    δ = 2 b − a 2 { x 1 = a + δ 2 x 1 = a − δ 2 \delta = \sqrt{2b-a^2}\\ \left\{\begin{aligned} & x_1 = \frac{a + \delta}{2} \\ & x_1 = \frac{a - \delta}{2} \end{aligned}\right. δ=2ba2 x1=2a+δx1=2aδ

代码

class Solution:
    def missingTwo(self, nums: List[int]) -> List[int]:
        n = len(nums)
        currentSum = sum(nums)
        currentSquareSum = sum(map(lambda x: x*x, nums))
        expectSum = (n+2)*(n+3)//2
        expectSquareSum = (n+2)*(n+3)*(2*n+5)//6
        a = expectSum - currentSum
        b = expectSquareSum - currentSquareSum
        delta = math.sqrt(2 * b - a * a)
        return [int((a + delta)//2), int((a-delta)//2)]
  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( 1 ) O(1) O(1)

题解5

思路

  • 对所有数和 [ 1 , n + 2 ] [1,n+2] [1,n+2]中的所有数位运算,最后得到的结果为 x 1 ⊕ x 2 x_1 \oplus x_2 x1x2
  • 因为python中负数使用补码表示,所以可以使用 x o r s u m ⊕ − x o r s u m xorsum \oplus -xorsum xorsumxorsum作为掩码进行分类
  • 分组后再通过掩码计算分别求出两个不同的数

代码

class Solution:
    def missingTwo(self, nums: List[int]) -> List[int]:
        xorSum = 0
        for n in nums:
            xorSum ^= n
        for n in range(1, len(nums) + 3):
            xorSum ^= n
        mask = xorSum & (-xorSum)
        n1, n2 = 0,0
        for n in nums:
            if n&mask:
                n1 ^= n
            else:
                n2 ^= n
        for n in range(1, len(nums) + 3):
            if n&mask:
                n1 ^= n
            else:
                n2 ^= n
        return [n1,n2]

复杂度

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( 1 ) O(1) O(1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pass night

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值