【Leetcode】Single Number

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

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

分析:抑或操作能够使得偶数个相同的数值结果变为0

class Solution {
public:
    int singleNumber(int A[], int n) {
        while(--n != 0)
            A[n - 1] ^= A[n];
            
        return A[0];
    }
};


你可能感兴趣的:(【Leetcode】Single Number)