当前位置: 首页 > news >正文

位运算题目:解码异或后的排列

文章目录

  • 题目
    • 标题和出处
    • 难度
    • 题目描述
      • 要求
      • 示例
      • 数据范围
  • 解法
    • 思路和算法
    • 代码
    • 复杂度分析

题目

标题和出处

标题:解码异或后的排列

出处:1734. 解码异或后的排列

难度

6 级

题目描述

要求

有一个整数数组 perm \texttt{perm} perm,是前 n \texttt{n} n 个正整数的排列,且 n \texttt{n} n奇数

它被加密成另一个长度为 n − 1 \texttt{n} - \texttt{1} n1 的整数数组 encoded \texttt{encoded} encoded,满足 encoded[i] = perm[i] ⊕ perm[i + 1] \texttt{encoded[i] = perm[i]} \oplus \texttt{perm[i + 1]} encoded[i] = perm[i]perm[i + 1]。例如,如果 perm = [1,3,2] \texttt{perm = [1,3,2]} perm = [1,3,2],那么 encoded = [2,1] \texttt{encoded = [2,1]} encoded = [2,1]

给定 encoded \texttt{encoded} encoded 数组,返回原始数组 perm \texttt{perm} perm。题目保证答案存在且唯一。

示例

示例 1:

输入: encoded = [3,1] \texttt{encoded = [3,1]} encoded = [3,1]
输出: [1,2,3] \texttt{[1,2,3]} [1,2,3]
解释:如果 perm = [1,2,3] \texttt{perm = [1,2,3]} perm = [1,2,3],那么 encoded = [1 ⊕ 2,2 ⊕ 3] = [3,1] \texttt{encoded = [1} \oplus \texttt{2,2} \oplus \texttt{3] = [3,1]} encoded = [12,23] = [3,1]

示例 2:

输入: encoded = [6,5,4,6] \texttt{encoded = [6,5,4,6]} encoded = [6,5,4,6]
输出: [2,4,1,5,3] \texttt{[2,4,1,5,3]} [2,4,1,5,3]

数据范围

  • 3 ≤ n < 10 5 \texttt{3} \le \texttt{n} < \texttt{10}^\texttt{5} 3n<105
  • n \texttt{n} n 是奇数
  • encoded.length = n − 1 \texttt{encoded.length} = \texttt{n} - \texttt{1} encoded.length=n1

解法

思路和算法

对于 0 ≤ i < n − 1 0 \le i < n - 1 0i<n1 encoded [ i ] = perm [ i ] ⊕ perm [ i + 1 ] \textit{encoded}[i] = \textit{perm}[i] \oplus \textit{perm}[i + 1] encoded[i]=perm[i]perm[i+1],利用按位异或的性质可以得到 encoded [ i ] ⊕ perm [ i ] = perm [ i + 1 ] ⊕ perm [ i ] ⊕ perm [ i ] = perm [ i + 1 ] \textit{encoded}[i] \oplus \textit{perm}[i] = \textit{perm}[i + 1] \oplus \textit{perm}[i] \oplus \textit{perm}[i] = \textit{perm}[i + 1] encoded[i]perm[i]=perm[i+1]perm[i]perm[i]=perm[i+1]。因此对于 1 ≤ i < n 1 \le i < n 1i<n perm [ i ] = encoded [ i − 1 ] ⊕ perm [ i − 1 ] \textit{perm}[i] = \textit{encoded}[i - 1] \oplus \textit{perm}[i - 1] perm[i]=encoded[i1]perm[i1]。如果可以知道 perm [ 0 ] \textit{perm}[0] perm[0] 的值,则可以解码得到完整的原始数组 perm \textit{perm} perm

由于数组 perm \textit{perm} perm 的长度 n n n 是奇数,因此除了 perm [ 0 ] \textit{perm}[0] perm[0] 以外的剩余元素个数 n − 1 n - 1 n1 是偶数,数组 encoded \textit{encoded} encoded 的长度 n − 1 n - 1 n1 是偶数。可以在数组 encoded \textit{encoded} encoded 中寻找 n − 1 2 \dfrac{n - 1}{2} 2n1 个元素,使得这些元素的异或结果为 perm [ 1 ] \textit{perm}[1] perm[1] perm [ n − 1 ] \textit{perm}[n - 1] perm[n1] 的异或结果。

由于 encoded [ i ] = perm [ i ] ⊕ perm [ i + 1 ] \textit{encoded}[i] = \textit{perm}[i] \oplus \textit{perm}[i + 1] encoded[i]=perm[i]perm[i+1],因此计算 encoded \textit{encoded} encoded 的所有奇数下标处的元素的异或结果,记为 oddXor \textit{oddXor} oddXor,则该异或结果为 encoded \textit{encoded} encoded n − 1 2 \dfrac{n - 1}{2} 2n1 个元素的异或结果,等于 perm [ 1 ] \textit{perm}[1] perm[1] perm [ n − 1 ] \textit{perm}[n - 1] perm[n1] 的异或结果。

totalXor \textit{totalXor} totalXor 表示 1 1 1 n n n 的所有整数的异或结果,则 totalXor = oddXor ⊕ perm [ 0 ] \textit{totalXor} = \textit{oddXor} \oplus \textit{perm}[0] totalXor=oddXorperm[0],利用按位异或的性质可以得到 totalXor ⊕ oddXor = oddXor ⊕ oddXor ⊕ perm [ 0 ] = perm [ 0 ] \textit{totalXor} \oplus \textit{oddXor} = \textit{oddXor} \oplus \textit{oddXor} \oplus \textit{perm}[0] = \textit{perm}[0] totalXoroddXor=oddXoroddXorperm[0]=perm[0],即 perm [ 0 ] \textit{perm}[0] perm[0] 的值等于 totalXor \textit{totalXor} totalXor oddXor \textit{oddXor} oddXor 的异或结果。

得到 perm [ 0 ] \textit{perm}[0] perm[0] 的值之后,从 i = 1 i = 1 i=1 i = n − 1 i = n - 1 i=n1 依次计算 perm [ i ] \textit{perm}[i] perm[i] 的值,即可得到原数组 perm \textit{perm} perm,且原数组的结果唯一。

代码

class Solution {public int[] decode(int[] encoded) {int n = encoded.length + 1;int[] perm = new int[n];int totalXor = 0;for (int i = 1; i <= n; i++) {totalXor ^= i;}int oddXor = 0;for (int i = 1; i < n; i += 2) {oddXor ^= encoded[i];}perm[0] = totalXor ^ oddXor;for (int i = 1; i < n; i++) {perm[i] = encoded[i - 1] ^ perm[i - 1];}return perm;}
}

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组 perm \textit{perm} perm 的长度。需要遍历 1 1 1 n n n 的所有整数计算总异或值,然后遍历长度为 n − 1 n - 1 n1 的数组 encoded \textit{encoded} encoded 两次得到原始数组 perm \textit{perm} perm

  • 空间复杂度: O ( 1 ) O(1) O(1)。注意返回值不计入空间复杂度。

相关文章:

  • PostgreSQL 数据库备份与恢复全面指南20250424
  • Dockerfile指令
  • 知识图谱火了?
  • 【Java面试笔记:进阶】16.synchronized底层如何实现?什么是锁的升级、降级?
  • 医学图像(DICOM数据)读取及显示(横断面、冠状面、矢状面、3D显示)为什么用ITK+VTK,单独用ITK或者VTK能实一样功能吗?
  • Spring Cloud Alibaba VS Spring Cloud
  • 如何将极狐GitLab 合并请求导出为 CSV?
  • 《Pinia 从入门到精通》Vue 3 官方状态管理 -- 基础入门篇
  • vue3+TS 手动实现表格滚动
  • 取模--特殊情况讨论/数论
  • 机器学习--线性回归模型
  • Unity InputSystem触摸屏问题
  • 使用Tauri 2.3.1+Leptos 0.7.8开发桌面小程序汇总
  • 优雅实现网页弹窗提示功能:JavaScript与CSS完美结合
  • PyQt6基础_QTabWidget
  • 新增优惠券
  • 哈希表的实现
  • 大模型AI的“双刃剑“:数据安全与可靠性挑战与破局之道
  • 高精度并行2D圆弧拟合(C++)
  • ORACLE RAC环境使用ASM机制零宕机时间更换存储的实践
  • 小马智行彭军:今年是Robotaxi量产元年,有望3年后盈亏平衡
  • 三亚亚龙湾3.4公里岸线近岸海域使用权挂牌出让,起始价近九千万
  • “70后”女博士张姿卸任国家国防科技工业局副局长
  • 波音CEO称中方因中美“贸易战”停止接收波音飞机,外交部回应
  • 集合多家“最美书店”,松江成立书店联盟“书香满云间”
  • 小鹏机器人IRON亮相上海车展,何小鹏:相信更多人形机器人会现身车展