Leetcode-Single Number

LiBlog發表於2014-12-23

Given an array of integers, every element appears twice except for one. Find that single one.

Analysis:

Use XOR operation.

Solution:

 1 public class Solution {
 2     public int singleNumber(int[] A) {
 3         if (A.length==0) return -1;
 4         int res = A[0];
 5         for (int i=1;i<A.length;i++)
 6             res = res^A[i];
 7         return res;
 8         
 9     }
10 }

 

相關文章