leetcode-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?

 

在数组中,数字皆成对出现,找出唯一一个单独的数字。

思路:a^a=0,相同数字异或为0.

 

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

 

posted @ 2017-01-16 10:06  鸭子船长  阅读(104)  评论(0编辑  收藏  举报