首页 > 代码库 > [LeetCode]Single Number

[LeetCode]Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

因为只有一个数是一个,其余都为2个,故采用一直异或下去就可以求得结果

class Solution {
public:
    int singleNumber(int A[], int n) {
        for(int i = 0; i < n; i++)
        {
            if(n == 1)
            {
                return A[0];
            }
            if(i == 0)
            {
                i++;
            }
            A[0] ^= A[i];
        }
        return A[0];
        
    }
};