首页 > 代码库 > 剑指offer40

剑指offer40

 

class Solution {
public:
    void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
        if(data.size() <= 0)
            return;
        int resultOR = 0;
        for(int i = 0;i < data.size();i++){
            resultOR ^= data[i];
        }
        int resultIndex = FindFirst1(resultOR);
        *num1 = 0;
        *num2 = 0;
        for(int i = 0;i < data.size();i++){
            if(Is1(data[i],resultIndex))
                *num1 ^= data[i];
            else
                *num2 ^= data[i];
        }
    }
    int FindFirst1(int num){
        int index = 0;
        while((num & 1) == 0){
            num = num >> 1;
            index++;
        }
        return index;
    }
    bool Is1(int number,int index){
        number = number >> index;
        return number & 1;
    }
};

(num&1) == 0这里必须打上括号,因为==的优先级高于&

return num&1可以不打括号

剑指offer40