0
点赞
收藏
分享

微信扫一扫

Leetcode: SingleNumber I


题目:

Given an array of integers, every element appearstwiceexcept for one. Find that single one.

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

解答:

int singleNumber(int A[], int n)
{
int ans = 0;
for (int i = 0; i < n; i++)
{
ans ^= A[i];//相同的数字经过异或运算结果为0
}
return ans;
}



于是,我深深领悟到了位运算符的魅力。

举报

相关推荐

0 条评论