文章目录
题目来源:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/decode-xored-permutation
题目如下
python角度分析
- encoded长度为n-1,根据规律得到perm数组的长度为n
- 假如perm=[a, b, c, d, e],那么encoded=[a ^ b, b ^ c, c ^ d, d ^ e]
- perm里面的元素都异或:P = p[0] ^ p[1] ^ p[2] ^ p[3] ^ p[4]= a ^ b ^ c ^ d ^ e
- encoded偶数项异或:E = b ^ c ^ d ^ e
- P ^ E = a
- 可以得到p[0]
- 以此类推,p[0] ^ e[0] = b …
代码如下:
class Solution:
def decode(self, encoded):
length=len(encoded)
perm=[]
E=0
P=0
for i in range(1,length+2):
P=P^i
for i in range(1,length,2):
E=E^encoded[i]
perm=[E^P]
for i in range(1,length+1):
perm.append(encoded[i-1]^perm[-1])
return perm