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?
题意
数列中有一个数出现了一次,其余数都出现了2次,找出这个出现一次的数
算法要求o(n)并且建议不使用多余内存空间
解题思路
神奇的异或世界
a^b^a = b(咳咳咳
AC代码
/**
class Solution {
public:
int singleNumber(int A[], int n) {
int tmp=0;
for(int i = 0 ; i < n; i++)
tmp^=A[i];
return tmp;
}
};