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

做网站找 汇搜网络河南最近的热搜事件

做网站找 汇搜网络,河南最近的热搜事件,如何查询网站的注册信息,上海有名的科技公司文章目录 题目标题和出处难度题目描述要求示例数据范围 解法思路和算法代码复杂度分析 题目 标题和出处 标题:解码异或后的排列 出处:1734. 解码异或后的排列 难度 6 级 题目描述 要求 有一个整数数组 perm \texttt{perm} perm,是前…

文章目录

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

题目

标题和出处

标题:解码异或后的排列

出处: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)。注意返回值不计入空间复杂度。

http://www.dtcms.com/wzjs/271730.html

相关文章:

  • 自己做的网站无法访问互动营销的概念
  • 哈尔滨企业建站短视频询盘获客系统
  • 做产品批发的网站有哪些seo是哪个英文的简写
  • 重庆网站开发推广方案如何写
  • 外贸网站定制公司百度优化
  • 潍坊做网站的网络营销师怎么考
  • 哪家公司做移动网站结构优化是什么意思
  • 网站改版建设主要免费发布信息的网站平台
  • 视频网站做推广有没有效果steam交易链接怎么看
  • wordpress模版制作seo教程自学
  • 实惠福步外贸论坛天津关键词优化网站
  • 武汉人才网重庆seo整站优化系统
  • 成立一个网站软件需要多少钱免费开源网站
  • 域名打不开网站排名网
  • 个人可以做网站推广什么网站百度收录快
  • 高校微信网站建设情况汇报优化标题关键词技巧
  • 运城做网站电话产品推广计划
  • 怎让做淘宝网站网站制作流程图
  • 做网站外包创业国外网站开发
  • python编程是干嘛的seo全称是什么意思
  • 网站空间和云服务器网络营销文案策划
  • 新网站推广方法最近几天的新闻
  • 英文网站的建设产品推广方案
  • 电影网站设计模板快优吧seo优化
  • 网站域名有了 网站如何建设google搜索免费入口
  • 辽阳网站seo推广一次多少钱
  • 小城市做网站seo百度快速排名软件
  • 哪个网站可以领手工回家做广州网站建设技术外包
  • 公司做两个网站苏州网站建设费用
  • 太原医疗网站建设软件培训机构