题目链接
https://leetcode.cn/problems/decode-xored-array/
知识点:异或
-
相同为0,相异为1
-
a ^ b ^ b = a
通过代码
class Solution {
public int[] decode(int[] encoded, int first) {
int n = encoded.length + 1;
int[] ans = new int[n];
ans[0] = first;
for (int i = 1; i < n; i++) {
ans[i] = encoded[i - 1] ^ ans[i - 1];
}
return ans;
}
}