1734. 解码异或后的排列
为保证权益,题目请参考 1734. 解码异或后的排列(From LeetCode).
解决方案1
Python
python
from typing import List
from functools import reduce
class Solution:
def decode(self, encoded: List[int]) -> List[int]:
total = reduce(lambda x, y: x ^ y, range(1, len(encoded) + 2))
newTotal = reduce(
lambda x, y: x ^ y, [encoded[i] for i in range(1, len(encoded), 2)]
)
ans = [0 for i in range(len(encoded) + 1)]
ans[0] = total ^ newTotal
for i in range(1, len(encoded) + 1):
ans[i] = encoded[i - 1] ^ ans[i - 1]
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17