Loading...

10-31 leetcode 2433

链接 2433. Find The Original Array of Prefix Xor

题目

You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:

pref[i] = arr[0] ^ arr[1] ^ … ^ arr[i].
Note that ^ denotes the bitwise-xor operation.

It can be proven that the answer is unique.

题解

这题是异或的基本性质,从 a^b=x 可以推出 x^a=b,那么我们可以从 pref 数组中推出 arr 数组

1
2
3
class Solution:
def findArray(self, pref: list[int]) -> list[int]:
return [pref[i] ^ pref[i - 1] if i > 0 else pref[i] for i in range(len(pref))]

Comment