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?
Note:
Easy one. But I just started to re-use the c++ so I forgot how to use the 'exclusive or' command in C++.
Marked as Level one since I know the algorithm.
Note:
1. The 'exclusive or' command in c++ is '^'
2. The iteration steps in c++ for vector is:
for(std::vector<int>::iterator i=nums.begin();i!=nums.end();++i){}
Answer:
class Solution { public: int singleNumber(vector<int>& nums) { int tmp=0; for(std::vector<int>::iterator i=nums.begin();i!=nums.end();++i){ tmp^=(*i); } return tmp; } };