异或运算

异或运算的特性:

  • 异或自己得0,任何数异或0得自己本身;
  • 具有交换律、结合律,例如 1^2^3^4^2^3^1 = (1^1)^(2^2)^(3^3)^4 = 0^0^0^4 = 0^4 = 4;

总结:异或运算擅长找不同。

例题(leetcode389找不同):

1 class Solution {
2 public:
3     char findTheDifference(string s, string t) {
4         vector<int> dp1(26, 0), dp2(26, 0);
5         for(int i = 0; i < s.size(); i++) t[0] ^= s[i];
6         for(int i = 1; i < t.size(); i++) t[0] ^= t[i];
7         return t[0];
8     }
9 };
原文地址:https://www.cnblogs.com/Arthas8086/p/13442345.html