136. Single Number

2017-06-21  本文已影响0人  YellowLayne

1.描述

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?

2.分析

x ^ x == 0

3.代码

int singleNumber(int* nums, int numsSize) {
    if (NULL == nums || numsSize <= 0) exit(-1);
    int single = nums[0];
    for (unsigned int i = 1; i < numsSize; ++i) {
        single ^= nums[i];
    }
    return single;
}
上一篇 下一篇

猜你喜欢

热点阅读