Single Number

来源:互联网 发布:北京赛车两期计划数据 编辑:程序博客网 时间:2024/06/06 16:47

https://leetcode.com/problems/single-number/

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?

异或运算性质:
1、a^a = 0;
2、0^a = a;
所以,如果对所有元素做异或运算,其结果为那个出现一次的元素,得解。

int singleNumber(int* nums, int numsSize) {    int i;    int ans = nums[0];    for(i = 1; i < numsSize; i++) {        ans ^= nums[i];    }    return ans;}
0 0
原创粉丝点击