【LeetCode数组篇 / 异或运算 / Java实现】1720. 解码异或后的数组(简单)


题目链接

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;

    }
}

版权声明:本文为guliguliguliguli原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。