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?

int singleNumber(int A[], int n) {
        int x;
        for(size_t i=0;i<n;++i)
        {
            x^=A[i];//异或两次同样的值为0,异或0为原值
        }
        return x;
    }


posted on 2017-05-13 12:56  yjbjingcha  阅读(89)  评论(0编辑  收藏  举报

导航