Cattle customer network - a digital array appear only to prove safety office-

Title: An integer array in addition to the two figures, other figures appear twice. Please write a program to find these two figures appear only.
Solution one:
ideas: First, we'll think of an array only one number appear that question once. We can use different or thinking.
But this question has two numbers appeared only once, we can still take advantage of exclusive-or thinking. We first need the original array is divided into two arrays, and the need to meet: the emergence of a number 1, only two appear in a different array. 2, the same two numbers can not be divided in a different array.
First, we can iterate over the array, each digital XOR. Since there are two different numbers, so the results do not necessarily tmp equal to 0. We can find out the results of a number of binary tmp is equal to the first position index 1, the use of which one can distinguish between two different numbers , and can secure the same numbers was in the same array. Code as follows:

class Solution {
public:
    void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
        int tmp=0;
        for(int i=0;i<data.size();++i)
            tmp=tmp^data[i];
        unsigned int index =findfirst1(tmp);
        *num1 = 0;
        *num2 = 0;
        for(int i=0;i<data.size();++i)
        {
            if(isbit1(data[i],index))
                num1[0]=num1[0]^data[i];
            else
                num2[0]=num2[0]^data[i];
        }
        }
    unsigned int findfirst1(int num)
    {
        int index = 0;
        while(((num&1)==0)&&(index<32))
        {
            num=num>>1;
            ++index;
        }
        return index;
    }
    int isbit1(int num,unsigned int index)
    {
        num=num>>index;
        return num&1==1;
    }
};

Solution two:
thinking: the establishment of a set container, and then through the array, if the number is already present in the set container, remove the number, if not present, then the digital set into the container, what is left is only a few appear the first two numbers.

class Solution {
public:
    void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
        set<int> tmp;
        for(int i=0;i<data.size();++i)
        {
            if(tmp.count(data[i])==1)
                tmp.erase(data[i]);
            else
                tmp.insert(data[i]);
        }
        vector<int> ans;
        for(set<int>::iterator iter = tmp.begin();iter!=tmp.end();++iter)
        {
            ans.push_back(*iter);
        }
        *num1 = ans[0];
        *num2 = ans[1];
    }
};

python's solution:
idea: use the idea of a second solution of c ++.

# -*- coding:utf-8 -*-
class Solution:
    # 返回[a,b] 其中ab是出现一次的两个数字
    def FindNumsAppearOnce(self, array):
        # write code here
        ans = []
        for num in array:
            if num in ans:
                ans.remove(num)
            else:
                ans.append(num)
        return ans

Guess you like

Origin blog.csdn.net/qq_43387999/article/details/91334704