本文主要是介绍2007. 从双倍数组中还原原数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2007. 从双倍数组中还原原数组
题目链接:2007. 从双倍数组中还原原数组
代码如下:
class Solution {
public:vector<int> findOriginalArray(vector<int>& changed) {sort(changed.begin(),changed.end());vector<int> res;unordered_map<int,int> count;for(const int& a:changed){count[a]++;//统计各个数的出现次数}for(const int &a:changed){if(count[a]==0)//这一步目的是:双倍数已经为0,跳过去即可 {continue;}count[a]--;if(count[a*2]==0)//a存在但a*2不存在证明不是双倍数组{return {};}count[a*2]--;res.push_back(a);}return res;}
};
这篇关于2007. 从双倍数组中还原原数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!