美文网首页
136. Single Number

136. Single Number

作者: YellowLayne | 来源:发表于2017-06-21 10:41 被阅读0次

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

相关文章

网友评论

      本文标题:136. Single Number

      本文链接:https://www.haomeiwen.com/subject/yppzqxtx.html