leetcode 1734. 解码异或后的排列(位运算)

给你一个整数数组 perm ,它是前 n 个正整数的排列,且 n 是个 奇数 。

它被加密成另一个长度为 n - 1 的整数数组 encoded ,满足 encoded[i] = perm[i] XOR perm[i + 1] 。比方说,如果 perm = [1,3,2] ,那么 encoded = [2,1] 。

给你 encoded 数组,请你返回原始数组 perm 。题目保证答案存在且唯一。

示例 1:

输入:encoded = [3,1]
输出:[1,2,3]
解释:如果 perm = [1,2,3] ,那么 encoded = [1 XOR 2,2 XOR 3] = [3,1]
示例 2:

输入:encoded = [6,5,4,6]
输出:[2,4,1,5,3]

解题思路

设x=1 ^ 2 ^ 3 ^ …^n
那么perm[0]=x ^ encoded[1] ^ encoded[3]…
因为encoded[i] = perm[i] XOR perm[i + 1],所以perm[i + 1]=encoded[i] XOR perm[i]
若知道了perm[0]自然可以推出perm[1],如此类推,就可以推出所有

推导

为什么可知perm[0]=x ^ encoded[1] ^ encoded[3]…?
因为encoded[1] = perm[1] XOR perm[2] ,encoded[3] = perm[3] XOR perm[4]…
因此可得encoded[1] ^ encoded[3]…= perm[1]^ perm[2]^ perm[3]^ perm[4]…

又因为perm是前 n 个正整数的排列,所以
perm[0]perm[1] perm[2]^ perm[3]^ perm[4]…=1 ^ 2 ^ 3 ^ …^n=x

因此可得perm[0]=x ^ encoded[1] ^ encoded[3]…

代码

func decode(encoded []int) []int {n:=len(encoded)+1odd:=0for i := 1; i < len(encoded) ; i+=2  {odd^=encoded[i]}all:=0for i := 1; i <=n ; i++ {all^=i}pre:=all^oddres := make([]int, n)res[0]=prefor j, code := range encoded {cur:=pre^coderes[j+1]=curpre=cur}return res
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391064.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ZooKeeper3.4.5-最基本API开发

2019独角兽企业重金招聘Python工程师标准>>> package cn.itcast.bigdata.zk;import java.io.IOException; import java.util.List;import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEven…

字符串转换整数python_将Python字符串转换为Int:如何在Python中将字符串转换为整数

字符串转换整数pythonUnlike many other programming languages out there, Python does not implicitly typecast integers (or floats) to strings when you concatenate them to strings.与现有的许多其他编程语言不同&#xff0c;Python在将整数连接到字符串时不会隐式地将…

理解Java里面的必检异常和非必检异常

问题&#xff1a;理解Java里面的必检异常和非必检异常 Joshua Bloch在"Effective Java"里面说过 在可恢复的条件下和编程错误导致的运行时错误时&#xff0c;使用必检异常&#xff08;第二版的第52页&#xff09; 让我们来看一下我对这个的正确理解吧 下面是我对…

使用vim打开文件的16进制形式,编辑和全文替换

1、先用vim打开文件的二进制形式&#xff0c;如果不以二进制可能会产生转换错误。 vim -b file-to-open.dat 2、用xxd把文件转换成十六进制格式 :%!xxd 现在就可以对待普通文本一样查看和编辑二进制文件了。 3、vim 单文件替换方法 :%s/old/new/gc 全文执行替换,询问是…

nlp自然语言处理_不要被NLP Research淹没

nlp自然语言处理自然语言处理 (Natural Language Processing) 到底是怎么回事&#xff1f; (What is going on?) NLP is the new Computer VisionNLP是新的计算机视觉 With enormous amount go textual datasets available; giants like Google, Microsoft, Facebook etc have…

opencv 随笔

装环境好累&#xff0c;python3.6&#xff0c;opencv3.4 好不容易装好了&#xff0c;结果 addweight的时候总是报错 The operation is neither array op array (where arrays have the same size and the same number of channels), nor array op scalar, nor scalar op array …

js打开飞行模式_什么是飞行模式? 它有什么作用?什么时候应该打开它?

js打开飞行模式If youve flown on an airplane in the last decade and you have a smart phone, youve likely had to put that phone in airplane mode before the plane takes off.如果您在过去的十年中乘坐过飞机&#xff0c;并且拥有一部智能手机&#xff0c;那么您可能必…

在Java 里面怎么比较字符串

问题&#xff1a;在Java 里面怎么比较字符串 到目前为止&#xff0c;我使用 操作符去比较字符串在我的程序里面。然而&#xff0c;却产生了一个bug&#xff0c;将这个改为了.equals()以后&#xff0c;就把bug修复了 是不是太辣鸡了&#xff1f;它什么时候应该被使用或者说是不…

中小型研发团队架构实践三要点(转自原携程架构师张辉清)

如果你正好处在中小型研发团队…… 中小型研发团队很多&#xff0c;而社区在中小型研发团队架构实践方面的探讨却很少。中小型研发团队特别是 50 至 200 人的研发团队&#xff0c;在早期的业务探索阶段&#xff0c;更多关注业务逻辑&#xff0c;快速迭代以验证商业模式&#xf…

时间序列预测 预测时间段_应用时间序列预测:美国住宅

时间序列预测 预测时间段1.简介 (1. Introduction) During these COVID19 months housing sector is rebounding rapidly after a downtime since the early months of the year. New residential house construction was down to about 1 million in April. As of July 1.5 mi…

zabbix之web监控

Web monitoring(web监控)是用来监控Web程序的&#xff0c;可以监控到Web程序的下载速度&#xff0c;返回码以及响应时间&#xff0c;还支持把一组连续的Web动作作为一个整体进行监控。 1.Web监控的原理 Web监控即对HTTP服务的监控&#xff0c;模拟用户去访问网站&#xff0c;对…

如何使用Webpack在HTML,CSS和JavaScript之间共享变量

Earlier this week, I read an article explaining how CSS-in-JS slows down the rendering of some React apps and how static CSS is faster. But CSS-in-JS is very popular because, among other features, you can style dynamically using JavaScript variables.本周初…

Java中获得了方法名称的字符串,怎么样调用该方法

问题&#xff1a; Java中获得了方法名称的字符串&#xff0c;怎么样调用该方法 如果我有以下两个变量 Object obj; String methodName "getName";在不知道obj的类的情况下&#xff0c;我怎么样才能调用该类的名叫methodName的方法呢&#xff1f; 这个方法被调用时…

经验主义 保守主义_为什么我们需要行动主义-始终如此。

经验主义 保守主义It’s been almost three months since George Floyd was murdered and the mass protests. Three months since the nationwide protests, looting and riots across America.距离乔治弗洛伊德(George Floyd)被谋杀和大规模抗议活动已经快三个月了。 全国抗议…

Begin

Hello everyone, Finally,a technician from feiyang help me solve the question. Even though it is not the linux version i want.emmm...linux mint a new one i dont know about it And, lets make the life regular and delicate转载于:https://www.cnblogs.com/lxc-run…

redis介绍以及安装

一、redis介绍 redis是一个key-value存储系统。和Memcached类似&#xff0c;它支持存储的values类型相对更多&#xff0c;包括字符串、列表、哈希散列表、集合&#xff0c;有序集合。 这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作&#xff0c;而且…

java python算法_用Java,Python和C ++示例解释的搜索算法

java python算法什么是搜索算法&#xff1f; (What is a Search Algorithm?) This kind of algorithm looks at the problem of re-arranging an array of items in ascending order. The two most classical examples of that is the binary search and the merge sort algor…

Java中怎么把文本追加到已经存在的文件

Java中怎么把文本追加到已经存在的文件 我需要重复把文本追加到现有文件中。我应该怎么办&#xff1f; 回答一 你是想实现日志的目的吗&#xff1f;如果是的话&#xff0c;这里有几个库可供选择&#xff0c;最热门的两个就是Log4j 和 Logback了 Java 7 对于一次性的任务&a…

python机器学习预测_使用Python和机器学习预测未来的股市趋势

python机器学习预测Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works withou…

线程系列3--Java线程同步通信技术

上一篇文章我们讲解了线程间的互斥技术&#xff0c;使用关键字synchronize来实现线程间的互斥技术。根据不同的业务情况&#xff0c;我们可以选择某一种互斥的方法来实现线程间的互斥调用。例如&#xff1a;自定义对象实现互斥&#xff08;synchronize("自定义对象")…