对于本题的意思是:
给出一数组,数组中其中有两个元素不是重复的,剩余的元素都是两个重复的,返回这两个不重复的元素
这样说来,这题就有些特殊了,首先这个数组个数肯定是偶数,我们可以首先用sort排序,则第一个单独数肯定是位于基数位
上的,而且相同的数都是挨着的,可以用^ 来消除。具体程序如下:
//author:liuyang
//Time:28ms
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int x=0,index=0;
vector<int>result;
int n=nums.size();
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++)
{
x^=nums[i];
if((i+1)%2==0 && x!=0 && index<1)
{
result.push_back(nums[i-1]);
x=0^nums[i];
index++;
}
}
result.push_back(x);
return result;
}
};