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 n=A[0];
for(int i=1;i<A.length;i++){
n^=A[i];
}
return n;
}
}主要运用到了异或运算的性质。
本文介绍了一种使用异或运算解决寻找数组中唯一出现一次元素的方法。该算法具备线性时间复杂度,并且不需要额外的内存开销。
724

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



