返回介绍

solution / 1700-1799 / 1734.Decode XORed Permutation / README_EN

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

1734. Decode XORed Permutation

中文文档

Description

There is an integer array perm that is a permutation of the first n positive integers, where n is always odd.

It was encoded into another integer array encoded of length n - 1, such that encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2], then encoded = [2,1].

Given the encoded array, return _the original array_ perm. It is guaranteed that the answer exists and is unique.

 

Example 1:

Input: encoded = [3,1]
Output: [1,2,3]
Explanation: If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]

Example 2:

Input: encoded = [6,5,4,6]
Output: [2,4,1,5,3]

 

Constraints:

  • 3 <= n < 105
  • n is odd.
  • encoded.length == n - 1

Solutions

Solution 1: Bitwise Operation

We notice that the array $perm$ is a permutation of the first $n$ positive integers, so the XOR of all elements in $perm$ is $1 \oplus 2 \oplus \cdots \oplus n$, denoted as $a$. And $encode[i]=perm[i] \oplus perm[i+1]$, if we denote the XOR of all elements $encode[0],encode[2],\cdots,encode[n-3]$ as $b$, then $perm[n-1]=a \oplus b$. Knowing the last element of $perm$, we can find all elements of $perm$ by traversing the array $encode$ in reverse order.

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

class Solution:
  def decode(self, encoded: List[int]) -> List[int]:
    n = len(encoded) + 1
    a = b = 0
    for i in range(0, n - 1, 2):
      a ^= encoded[i]
    for i in range(1, n + 1):
      b ^= i
    perm = [0] * n
    perm[-1] = a ^ b
    for i in range(n - 2, -1, -1):
      perm[i] = encoded[i] ^ perm[i + 1]
    return perm
class Solution {
  public int[] decode(int[] encoded) {
    int n = encoded.length + 1;
    int a = 0, b = 0;
    for (int i = 0; i < n - 1; i += 2) {
      a ^= encoded[i];
    }
    for (int i = 1; i <= n; ++i) {
      b ^= i;
    }
    int[] perm = new int[n];
    perm[n - 1] = a ^ b;
    for (int i = n - 2; i >= 0; --i) {
      perm[i] = encoded[i] ^ perm[i + 1];
    }
    return perm;
  }
}
class Solution {
public:
  vector<int> decode(vector<int>& encoded) {
    int n = encoded.size() + 1;
    int a = 0, b = 0;
    for (int i = 0; i < n - 1; i += 2) {
      a ^= encoded[i];
    }
    for (int i = 1; i <= n; ++i) {
      b ^= i;
    }
    vector<int> perm(n);
    perm[n - 1] = a ^ b;
    for (int i = n - 2; ~i; --i) {
      perm[i] = encoded[i] ^ perm[i + 1];
    }
    return perm;
  }
};
func decode(encoded []int) []int {
  n := len(encoded) + 1
  a, b := 0, 0
  for i := 0; i < n-1; i += 2 {
    a ^= encoded[i]
  }
  for i := 1; i <= n; i++ {
    b ^= i
  }
  perm := make([]int, n)
  perm[n-1] = a ^ b
  for i := n - 2; i >= 0; i-- {
    perm[i] = encoded[i] ^ perm[i+1]
  }
  return perm
}

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

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

发布评论

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