返回介绍

solution / 2400-2499 / 2433.Find The Original Array of Prefix Xor / README_EN

发布于 2024-06-17 01:03:06 字数 4254 浏览 0 评论 0 收藏 0

2433. Find The Original Array of Prefix Xor

中文文档

Description

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.

 

Example 1:

Input: pref = [5,2,0,3,1]
Output: [5,7,2,3,2]
Explanation: From the array [5,7,2,3,2] we have the following:
- pref[0] = 5.
- pref[1] = 5 ^ 7 = 2.
- pref[2] = 5 ^ 7 ^ 2 = 0.
- pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3.
- pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.

Example 2:

Input: pref = [13]
Output: [13]
Explanation: We have pref[0] = arr[0] = 13.

 

Constraints:

  • 1 <= pref.length <= 105
  • 0 <= pref[i] <= 106

Solutions

Solution 1: Bit Manipulation

According to the problem statement, we have equation one:

$$ pref[i]=arr[0] \oplus arr[1] \oplus \cdots \oplus arr[i] $$

So, we also have equation two:

$$ pref[i-1]=arr[0] \oplus arr[1] \oplus \cdots \oplus arr[i-1] $$

We perform a bitwise XOR operation on equations one and two, and get:

$$ pref[i] \oplus pref[i-1]=arr[i] $$

That is, each item in the answer array is obtained by performing a bitwise XOR operation on the adjacent two items in the prefix XOR array.

The time complexity is $O(n)$, where $n$ is the length of the prefix XOR array. Ignoring the space consumption of the answer, the space complexity is $O(1)$.

class Solution:
  def findArray(self, pref: List[int]) -> List[int]:
    return [a ^ b for a, b in pairwise([0] + pref)]
class Solution {
  public int[] findArray(int[] pref) {
    int n = pref.length;
    int[] ans = new int[n];
    ans[0] = pref[0];
    for (int i = 1; i < n; ++i) {
      ans[i] = pref[i - 1] ^ pref[i];
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> findArray(vector<int>& pref) {
    int n = pref.size();
    vector<int> ans = {pref[0]};
    for (int i = 1; i < n; ++i) {
      ans.push_back(pref[i - 1] ^ pref[i]);
    }
    return ans;
  }
};
func findArray(pref []int) []int {
  n := len(pref)
  ans := []int{pref[0]}
  for i := 1; i < n; i++ {
    ans = append(ans, pref[i-1]^pref[i])
  }
  return ans
}
function findArray(pref: number[]): number[] {
  let ans = pref.slice();
  for (let i = 1; i < pref.length; i++) {
    ans[i] = pref[i - 1] ^ pref[i];
  }
  return ans;
}
impl Solution {
  pub fn find_array(pref: Vec<i32>) -> Vec<i32> {
    let n = pref.len();
    let mut res = vec![0; n];
    res[0] = pref[0];
    for i in 1..n {
      res[i] = pref[i] ^ pref[i - 1];
    }
    res
  }
}
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* findArray(int* pref, int prefSize, int* returnSize) {
  int* res = (int*) malloc(sizeof(int) * prefSize);
  res[0] = pref[0];
  for (int i = 1; i < prefSize; i++) {
    res[i] = pref[i - 1] ^ pref[i];
  }
  *returnSize = prefSize;
  return res;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文