程序员

找单数

2018-11-26  本文已影响0人  静水流深ylyang

版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


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?

#include<iostream>
using namespace std;
int singleNumber(int A[], int n)
{
    int num = 0;
    for(int i = 0; i < n; i++)
    {
        num ^= A[i];
    }
    return num;
}
int main()
{
    int A[] = {1, 1, 2, 2, 3, 3, 4};
    cout<<singleNumber(A, 7)<<endl;
    return 0;
}

版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


上一篇下一篇

猜你喜欢

热点阅读