欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

牛客网——数组中只出现一次的数字(C++)

程序员文章站 2022-03-08 15:52:35
...

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。

C++

class Solution {
public:
    void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) 
    {
        
        map<int,int> tmp;
        for(auto it:data)
        {
            tmp[it]++;
        }
        int flag=0;
        for(auto it:tmp)
        {
            if(it.second==1)
            {
                if(0==flag)
                {
                    *num1=it.first;
                    flag=1;
                }
                else
                {
                    *num2=it.first;
                }
            }
        }
    }
};