1.问题描述
Given an array of integers, every element appears twice except for one. Find that single one.Your algorithm should have a linear runtime complexity.
给定一组整数,除了一个元素外,每个元素都出现两次。找到那一个。你的算法应该具有线性运行时复杂性。
2.代码时刻
//c++ VS2013
#include<iostream>
using namespace std;
int singleNumber(int A[],int n) {
int result = 0;
for (int i = 0; i<n; i++)
{
result ^= A[i];//重点用到了异或判断的方法
}
return result;
}
int main()
{
int A[100] = { 1, 2, 3, 4,6, 15, 1, 2, 3, 4, 6 };
cout << singleNumber(A,13);
cout << endl;
return 0;
}