题目描述
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?
代码:
public class Solution {
public int singleNumber(int[] A) {
int a = 0;
for(int i = 0 ; i < A.length; i++)
{
a ^= A[i];
}
return a;
}
}
首先定义一个数0,使这个数和数组中的元素进行亦或运算,最终得到答案.