136. 只出现一次的数字

题目

代码

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int temp=0;
        for(auto i:nums)
        {
            temp^=i;
        }
        return temp;
        //每个数字和自己异或的结果为0,所以唯一的一个数字会被留下来
    }
};

思路

只需要知道一个原理,异或(两个数字不同则为1)

因此,如果两个重复的数字 1101 与1101异或则会为0。

https://github.com/li-zheng-hao
原文地址:https://www.cnblogs.com/lizhenghao126/p/11053652.html