1.从双倍数组中还原原数组(力扣,vector)
java式c++解法。
class Solution {
public:
vector<int> findOriginalArray(vector<int>& changed) {
int n = changed.size();
if(n % 2 == 1) return {};
map<int, int> mp;
for(int c : changed) mp[c]++;
sort(changed.begin(), changed.end());
vector<int> ans;
int i = 0;
while(i < n){
int x = changed[i];
if(mp[x] > 0){
mp[x]--;
if(mp[x * 2] <= 0) return {};
mp[x * 2]--;
ans.push_back(x);
}
i++;
}
return ans;
}
};