Single Number

Single Number

 Total Accepted: 6830 Total Submissions: 14936My Submissions

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

Note:
Your algorithm should have a linear runtime c


位操作, 找出数组中的单数, 双数是成对的数, 单数是只有一个数, 如 1 1 2 2 3 3 5 5 4

单数就是4

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


posted @ 2013-12-14 03:05  海滨银枪小霸王  阅读(128)  评论(0编辑  收藏  举报