LeetCode 题解工作台

相邻值的按位异或

下标从 0 开始、长度为 n 的数组 derived 是由同样长度为 n 的原始 二进制数组 original 通过计算相邻值的 按位异或(⊕) 派生而来。 特别地,对于范围 [0, n - 1] 内的每个下标 i : 如果 i = n - 1 ,那么 derived[i] = original[i…

category

2

题型

code_blocks

8

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·结合·位运算·操作

bolt

答案摘要

我们不妨假设原始二进制数组为 ,派生数组为 ,那么有: $$

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·结合·位运算·操作 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

下标从 0 开始、长度为 n 的数组 derived 是由同样长度为 n 的原始 二进制数组 original 通过计算相邻值的 按位异或(⊕)派生而来。

特别地,对于范围 [0, n - 1] 内的每个下标 i

  • 如果 i = n - 1 ,那么 derived[i] = original[i] ⊕ original[0]
  • 否则 derived[i] = original[i] ⊕ original[i + 1]

给你一个数组 derived ,请判断是否存在一个能够派生得到 derived有效原始二进制数组 original

如果存在满足要求的原始二进制数组,返回 true ;否则,返回 false

  • 二进制数组是仅由 01 组成的数组。

 

示例 1:

输入:derived = [1,1,0]
输出:true
解释:能够派生得到 [1,1,0] 的有效原始二进制数组是 [0,1,0] :
derived[0] = original[0] ⊕ original[1] = 0 ⊕ 1 = 1 
derived[1] = original[1] ⊕ original[2] = 1 ⊕ 0 = 1
derived[2] = original[2] ⊕ original[0] = 0 ⊕ 0 = 0

示例 2:

输入:derived = [1,1]
输出:true
解释:能够派生得到 [1,1] 的有效原始二进制数组是 [0,1] :
derived[0] = original[0] ⊕ original[1] = 1
derived[1] = original[1] ⊕ original[0] = 1

示例 3:

输入:derived = [1,0]
输出:false
解释:不存在能够派生得到 [1,0] 的有效原始二进制数组。

 

提示:

  • n == derived.length
  • 1 <= n <= 105
  • derived 中的值不是 0 就是 1
lightbulb

解题思路

方法一:位运算

我们不妨假设原始二进制数组为 aa,派生数组为 bb,那么有:

b0=a0a1b1=a1a2bn1=an1a0b_0 = a_0 \oplus a_1 \\ b_1 = a_1 \oplus a_2 \\ \cdots \\ b_{n-1} = a_{n-1} \oplus a_0

由于异或运算满足交换律和结合律,因此有:

b0b1bn1=(a0a1)(a1a2)(an1a0)=0b_0 \oplus b_1 \oplus \cdots \oplus b_{n-1} = (a_0 \oplus a_1) \oplus (a_1 \oplus a_2) \oplus \cdots \oplus (a_{n-1} \oplus a_0) = 0

因此,只要派生数组的所有元素的异或和为 00,就一定存在一个满足要求的原始二进制数组。

时间复杂度 O(n)O(n),其中 nn 为数组长度。空间复杂度 O(1)O(1)

1
2
3
4
class Solution:
    def doesValidArrayExist(self, derived: List[int]) -> bool:
        return reduce(xor, derived) == 0
speed

复杂度分析

指标
时间complexity is O(n) because each element is visited exactly once. Space complexity is O(1) as no extra arrays are needed; only a running XOR accumulator is required.
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Watch for understanding that each original element participates in two XOR operations.

  • question_mark

    Check if candidate handles circular array indexing correctly.

  • question_mark

    Ensure the solution does not attempt exhaustive original array construction, which is unnecessary.

warning

常见陷阱

外企场景
  • error

    Forgetting that XORing the same element twice cancels it out in cumulative XOR checks.

  • error

    Neglecting the circular property when computing derived[n-1] XOR original[0].

  • error

    Attempting to reconstruct original array explicitly, which wastes time and space.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Instead of binary arrays, consider arrays with small integer values, still using neighboring XOR constraints.

  • arrow_right_alt

    Allow derived arrays to have unknown entries represented by -1 and check if any valid original exists.

  • arrow_right_alt

    Compute the number of possible original arrays that can generate the given derived array.

help

常见问题

外企场景

相邻值的按位异或题解:数组·结合·位运算·操作 | LeetCode #2683 中等