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

php网站开发实验报告上海企业建站流程

php网站开发实验报告,上海企业建站流程,温州做网站优化,深圳营销型企业网站文章目录 题目标题和出处难度题目描述要求示例数据范围 解法思路和算法代码复杂度分析 题目 标题和出处 标题:解码异或后的排列 出处: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/a/456176.html

相关文章:

  • 网站维护成本网站维护的作用
  • 济南营销型网站网站备案 接入商名称
  • 哈尔滨专业网站营销秦皇岛陵县网站建设
  • 网站建站网站设计公司视频网站开发是什么
  • php网站建设公司网站设计论文的题目
  • 中小型企业建设一个网站大概需要多少钱肥西县建设发展局网站
  • 静安区品牌网站建设电子商务网站设计与维护
  • 网站开发团队分工wordpress标签怎么做静态化
  • 中国建设银行网站u盾修改密码wordpress密钥
  • 江西建设厅官方网站哈尔滨制作网站工作室
  • 网站管理过程百度怎样注册免费的网站
  • 网站营销型企业销售平台如何利用影视网站做cpa
  • 安徽和住房建设厅网站从58做网站怎么做
  • 郑州网站顾问iis服务器怎么部署php网站
  • 网站搭建就来徐州百都网络非常好建设网站联系方式
  • 唐山建设网站制作网站策划建设方案书
  • 怎么创建网站要钱吗php网站优化
  • 网站后期维护流程模板网站价格表
  • 定制网站制作平台wordpress 获取当前分类id
  • 备案网站建设方案书范文企业网站建设排名官网
  • 网站开发设计的阶段环球影城半年卡怎么预约
  • 做棋牌网站建设校园网站建设报价
  • 网站模板可视化编辑低代码建站
  • 虚拟资源站码支付wordpress网站二级菜单是什么原因
  • php网站开发教程下载新乡seo顾问
  • 公司网站建设素材购物网站静态页面
  • 请简述企业网站建设的流程seo培训课程
  • 网站建设目前流行什么公司网站建设的市场需求
  • 郑州做网站排名公司哪家好dw做的个人网站
  • 网站建设中源代码网站建设 昆山