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?
解题思路:使用异或,数组中相同的数两两异或全部为0,最后剩下的就是那个落单的数。
public class Solution {
public int singleNumber(int[] A) {
int n=0;
for(int i=0;i<A.length;i++)
{
n=n^A[i];
}
return n;
}
}
网友评论