Wednesday, January 1, 2014

Single Number

Single Number
Total Accepted: 7988 Total Submissions: 17558 My Submissions
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?
hint:
1^1=0;
1^1^5^5^3=3
public class Solution {
public int singleNumber(int[] A) {
if (A==null||A.length==0){
return -1;// throw Enception
}
int result=A[0];
for (int i=1; i<A.length; i++){
result^=A[i];
}
return result;
}
}

No comments:

Post a Comment