难道就我这样想的吗(hahaha)
时间复杂度是 O(nlogn)
,空间复杂度是 O(n)
。
class Solution {
public:
vector<int> findNumsAppearOnce(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<int> temp(nums);
sort(temp.begin(),temp.end());
nums.erase(unique(nums.begin(),nums.end()),nums.end());
vector<int>ans;
set_difference(temp.begin(),temp.end(),nums.begin(),nums.end(),inserter(ans,ans.begin()));
vector<int>res;
set_difference(nums.begin(),nums.end(),ans.begin(),ans.end(),inserter(res,res.begin()));
return res;
}
};