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?
异或的经典题目。
class Solution {
public:
int singleNumber(int A[], int n) {
int result = 0;
for(int ii = 0; ii < n; ii ++) {
result ^= A[ii];
}
return result;
}
};
本文介绍了一个经典算法问题:在一个除了一个元素外所有元素都出现两次的整数数组中找到那个只出现一次的元素。该问题可以通过异或操作解决,并且算法具有线性时间复杂度,不使用额外内存。
209

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



