Single Number
描述
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?
分析
异或,不仅能处理两次的情况,只要出现偶数次,都可以清零.
异或 x^x=0 , x^0=x
代码
1 // LeetCode, Single Number 2 class Solution { 3 public static int singleNumber(int A[]) { 4 for(int i=1;i<A.length;i++){ 5 A[i]^=A[i-1]; 6 } 7 return A[A.length-1]; 8 }