My solution:
Use unordered_map
Traverse the array, add the map before the element appears, erase it when it appears, and finally return the remaining two numbers
But this does not meet the requirements of space complexity
class Solution {
public:
vector<int> singleNumbers(vector<int>& nums) {
unordered_map<int,int> m;
vector<int> res;
for(int i=0;i<nums.size();i++){
if(m.count(nums[i])) m.erase(nums[i]);
else m[nums[i]]=1;
}
unordered_map<int,int>::iterator it;
it=m.begin();
res.push_back(it->first);
it++;
res.push_back(it->first);
return res;
}
};
Some bigwigs use XOR operations, and I will come back later. . .