当前位置:网站首页>leetcode1720_ 2021-10-14

leetcode1720_ 2021-10-14

2022-06-24 21:53:00 Programming rookie

leetcode1720_ Decode the XOR array

This is a very simple topic . The purpose of this blog is to introduce the characteristics of XOR symbol .
Some basic theorems about XOR operation :
XOR satisfies the law of association ,(a ^ b) ^ c = a ^ (b ^ c)
XOR satisfies the law of exchange ,a ^ b = b ^ a
Any number is exclusive or with itself 0,a^a = 0
Any number XOR 0 To oneself ,a^0 = a

that encoded[i] = arr[i] ^ arr[i + 1], Then there are arr[i + 1] = encoded[i] ^ ar[i].

class Solution {
    
public:
    vector<int> decode(vector<int>& encoded, int first) {
    
        vector<int> arr(encoded.size() + 1);
         arr[0] = first;
        for(int i = 0; i < encoded.size(); ++i){
    
            arr[i + 1] = encoded[i] ^ arr[i];
        }
        return arr;
    }
};

There are a few classic questions about XOR :( The following topics will be updated from time to time )
leetcode Exchange numbers —— Super classic title

原网站

版权声明
本文为[Programming rookie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241448513283.html