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?
异或,很简单
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Reverse;
/**
*
* @author suisuihan
*/
public class GetSingleNumber {
public int singleNumber(int[] A) {
int length = A.length;
if (length == 0) {
return 0;
}
int ret = 0;
for (int i = 0; i < length; i++) {
ret = ret ^ A[i];
}
return ret;
}
public static void main(String[] args) {
GetSingleNumber test = new GetSingleNumber();
int[] A = {3,1,1};
System.out.println(test.singleNumber(A));
}
}