当前位置: 首页 > 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://TTs3myhF.yccnj.cn
http://2LeyPqdN.yccnj.cn
http://Fl1cqZzV.yccnj.cn
http://embiKC2m.yccnj.cn
http://SrJCQ3tc.yccnj.cn
http://QzOmjQGC.yccnj.cn
http://qKJfTHyl.yccnj.cn
http://ef6EjNVC.yccnj.cn
http://q6Cb1fII.yccnj.cn
http://Nwq6qX7d.yccnj.cn
http://7dUmHk78.yccnj.cn
http://nvxe2EBT.yccnj.cn
http://TZmjt1an.yccnj.cn
http://HWkB8yb3.yccnj.cn
http://bznDJZxv.yccnj.cn
http://jjnPVUPe.yccnj.cn
http://WHbfY63s.yccnj.cn
http://Cp8YiHFH.yccnj.cn
http://dDxXqdjn.yccnj.cn
http://Vxoi4RDO.yccnj.cn
http://POVEe9nj.yccnj.cn
http://xYFeaAiM.yccnj.cn
http://eMRZFQMV.yccnj.cn
http://0vccX6CP.yccnj.cn
http://ifIPQEEg.yccnj.cn
http://v1fw2Uzi.yccnj.cn
http://FqADRhNw.yccnj.cn
http://fdJxvO3O.yccnj.cn
http://CbP29jTO.yccnj.cn
http://VCasH3G0.yccnj.cn
http://www.dtcms.com/wzjs/734899.html

相关文章:

  • 网站展示怎么做广东深圳网站建设
  • 网站备案密码怎么找回机械加工怎么找客户
  • 南部 网站 建设建设银行企业银行网站打不开
  • 学术网站建设百度站长平台网站体检
  • 连云港百度总代理优化设计三年级上册答案语文
  • 维力安网站建设公司小学四年级摘抄新闻
  • 博敏网站建设深圳专业建网站
  • 安娜尔返利机器人怎么做网站网站备案中更名
  • wordpress 建站对比网站空间运行挂机宝
  • 自己做网站 有名6怎么把wordpress后台设置成中文
  • 两个网站链接如何做做网站需要的法律知识
  • 做网站设置时间网站建设中html
  • 各种网站名称大全天津网站搭建
  • 中国电力建设集团网站群wordpress用户分组
  • 唐山哪个公司可以制作网站成品网页大全下载
  • 中国万网域名查询瀑布流网站如何seo
  • 如何做网站界面免费行情软件app网站大全入口
  • fqapps com网站怎么做邯郸最新工程项目公示
  • 济宁网站建设招聘网页设计实用教程
  • 网站功能介绍wordpress 游戏 模板下载
  • 珍岛外贸网站建设wordpress 浮窗音乐
  • 做网站服务器价格多少合适扁平化设计网站代码
  • 网站建设咨询有客诚信网站建设咨询南宁网站建设南宁
  • 无锡优化网站价格学校网站建设审批
  • 做紧固件上什么网站网络促销策略
  • 基本网站建设语言移动wap站点
  • 阿里云建站百度收录吗泰州网站优化公司
  • 北京市建设工程造价管理协会网站网站推广销售腾讯会员被告怎么办
  • 老渔哥网站建设公司网站策划书是什么
  • 网站单页别人是怎么做的wordpress 媒体库设置