Given an array of integers, every element appears twice except for one. Find that single one.
Note:
在代码中使用了“^”运算符,是一种位运算符,表示按位异或。详细介绍如下:
在C/C++中是按位异或键。如1010^1111=0101(二进制)
"^"是一种位逻辑运算符
^ -----按位异或(Xor)是一种逻辑运算,对应位相同时为假,不同时为真。
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 i,num;
for(num=0,i=0;i<n;i++)
num=num^A[i];
return num;
}
};
在代码中使用了“^”运算符,是一种位运算符,表示按位异或。详细介绍如下:
在C/C++中是按位异或键。如1010^1111=0101(二进制)
"^"是一种位逻辑运算符
^ -----按位异或(Xor)是一种逻辑运算,对应位相同时为假,不同时为真。