#include <unordered_map> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型vector * @return int整型vector */ vector<int> FindNumsAppearOnce(vector<int>& array) { // write code here int n = array.size(); vector<int> ans; unordered_map<int, int> mp; // unordered_map<int, int> mp2; for(int i = 0; i<n; ++i) { mp[array[i]] ++; } // 遍历哈希 for(auto [x, y]:mp) { if(y==1) { ans.push_back(x); } } if(ans[0]>ans[1]) swap(ans[0], ans[1]); return ans; } };
自己的方法 但似乎空间复杂度不满足