Single Number

链接:http://oj.leetcode.com/problems/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?

解法: 可以根据异或运算的配对性定理可以解此题.配对性定理可以详见:http://wenku.baidu.com/view/8bb52ffb700abb68a982fbba.html

 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         // Note: The Solution object is instantiated only once and is reused by each test case.
 5         if(n==0)  
 6         return 0;  
 7     int result=A[0];  
 8     for(int i=1;i<n;i++)  
 9     {  
10         result^=A[i];  
11     }  
12     return result;  
13     }
14 };
原文地址:https://www.cnblogs.com/hbiner/p/3393806.html