您的位置:首页 > 其它

LeetCode:Single Number

2014-09-11 15:13 274 查看
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?
<span style="font-size:18px;">int singleNumber(int A[], int n)
{
int result = 0;
for(int i = 0;i<n;i++)
result ^= A[i];
return result;
}</span>
偶数个相同的整数异或后为0,所以遍历数组,与所有的数组元素相继异或,最后异或的结果就是单独 的整数
已AC 36ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode