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;
    }
};


每天早上叫醒你的不是闹钟,而是心中的梦~
原文地址:https://www.cnblogs.com/vintion/p/4116993.html