Leetcode-Single Number

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 }

 

posted @ 2014-12-23 04:46  LiBlog  阅读(116)  评论(0编辑  收藏  举报