异或
class Solution {
public:
vector<int> singleNumbers(vector<int>& nums) {
int ret=0;
int n=nums.size();
for(int i:nums)ret=ret^i;
int div=1;
while((ret&div)==0)div=div<<1;
vector<int>out(2,0);
for(int i=0;i<n;i++){
if(nums[i]&div)out[0]=out[0]^nums[i];
else out[1]=out[1]^nums[i];
}
return out;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e04f0a1618bd4df8105d5f386e4f2657.png)
哈希表
class Solution {
public:
vector<int> constructArr(vector<int>& a) {
if(a.empty())return a;
int n=a.size();
vector<int>hashmap1(n);
vector<int>hashmap2(n);
hashmap1[0]=1;
hashmap2[0]=1;
for(int i=1;i<n;i++){
hashmap1[i]=hashmap1[i-1]*a[i-1];
}
int temp_right=1;
for(int i=n-1;i>-1;i--){
hashmap1[i]=hashmap1[i]*temp_right;
temp_right=temp_right*a[i];
}
return hashmap1;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3ce8cd3a958d8472e117dce2a9c3fa99.png)