一個整型數組裏除了兩個數字以外,其餘的數字都出現了兩次。請寫程序找出這兩個只出現一次的數字。

// test02.cpp : 定義控制檯應用程序的入口點。
//ios

#include "stdafx.h"
#include<iostream>
#include<vector>
using namespace std;


class Solution {
public:
    void FindNumsAppearOnce(vector<int> data, int* num1, int *num2) {
        int flag = 0;
        int a = 0;
        int num1flag = 0;

        for (int i = 0;i < data.size();i++)
        { 
            flag = 0;
            for (int j = 0;j < data.size();j++)//注意,此時的i不能等於j.注意j的下表,從1開始,而不是從i+1開始
            { 
                if (i == j) continue;
                if (data[i] == data[j])
                {
                //  cout << data[i] << endl;
                    flag = 1;
        //          break;
                }
            }
        
            
            if (flag == 0 && num1flag==0 )
            {
                *num1 = data[i];
        //      cout << data[i] << endl;
                num1flag=1;
                flag = 1;
            }
                
             if (flag == 0 && num1flag == 1)
            {
                *num2 = data[i];
            //  cout << data[i] << endl;
                
            }
        }

        //cout << "num1:" << *num1 << endl;
        //cout << "num2:" << *num2 << endl;
    }
};

int main()
{
    vector<int> vec = {1,2,3,4,5,6,7,7,6,5,1,2};
    Solution so;
    int i = 0, j = 0;

    int *num1=&i;
    int *num2=&j;
    so.FindNumsAppearOnce(vec, num1, num2);



    cout << endl;

return 0;
}
相關文章
相關標籤/搜索