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的特点就可以了,方法就是——异或!!!
此题是LeetCode中AC率暂时最高的一题。
代码如下:
class Solution {
public:
int singleNumber(int A[], int n) {
int res = 0;
for (int i=0; i<n; i++)
{
res = res ^ A[i];
}
return res;
}
};
本文介绍了一个LeetCode中的经典问题——寻找数组中只出现一次的元素,并提供了一种高效的解决方案,即通过异或操作实现。该方法不仅简单易懂,而且满足线性时间复杂度的要求。
3734

被折叠的 条评论
为什么被折叠?



