[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 public:
 3     int singleNumber(int A[], int n) {
 4         
 5         int res = 0;
 6         
 7         for(int i = 0; i<n; i++)
 8         {
 9             res = res ^ A[i];
10         }
11         
12         return res;
13     }
14 };

 

posted @ 2014-06-27 22:45  穆穆兔兔  阅读(141)  评论(0编辑  收藏  举报