3-line of easy-to-understand XOR method in C++


  • 0
    D

    XOR every character, the added one will not be canceled to 0.

        char findTheDifference(string s, string t) {
            int sum = 0;
            for (int i = 0; i < s.length(); ++i) sum ^= t[i] ^ s[i];
            return sum ^ t.back();
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.