LeetCode-Single Number

Given an array of integers, all elements are repeated twice except for one. Find that single one.

Could you implement it without using extra memory?

牢记异或,愿异或母亲忽悠着你

class Solution {
public:
    int singleNumber(int A[], int n) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(n<1)return 0;
        int c=A[0];
        for(int i=1;i<n;i++){
            c^=A[i];
        }
        return c;
    }
};
View Code

 

posted @ 2013-10-02 16:35  懒猫欣  阅读(153)  评论(0编辑  收藏  举报