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 if(n==0) 2 return 0; 3 int result=A[0]; 4 for(int i=1;i<n;i++) 5 { 6 result^=A[i]; 7 } 8 return result;