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

思路:

这题没什么难度,只要知道异或的操作就可以了

题解:

 1 class Solution 
 2 {
 3 public:
 4     int singleNumber(int A[], int n) 
 5     {
 6         int single = A[0];
 7         for(int i=1;i<n;i++)
 8             single = single ^ A[i];
 9         return single;
10     }
11 };

 

posted on 2014-12-01 10:43  cha1992  阅读(105)  评论(0编辑  收藏  举报

导航