题解 | #数组中只出现一次的两个数字#

数组中只出现一次的两个数字

https://www.nowcoder.com/practice/389fc1c3d3be4479a154f63f495abff8

class Solution {
public:
    vector<int>ans;
    vector<int>one;
    vector<int>another;
    vector<int> FindNumsAppearOnce(vector<int>& array) {
        int t = 0;
        for (int i : array) {
            t = t ^ i;
        }
        //t就是分类标准
        int time = 0;
        while (t != 1) {
            t /= 2;
            time++;
        }
        for (int i : array) {
            int x = i >> time;
            x = x & 1;
            if (x == 1) 
                one.push_back(i);
            else 
                another.push_back(i);

        }
        t = 3 ^ 1;
        t = 0;
        for (int i : one) {
            t = t ^ i;
        }
        ans.push_back(t);
        t = 0;
        for (int i : another) {
            t = t ^ i;
        }
        ans.push_back(t);
        sort(ans.begin(),ans.end());
        return ans;
    }

};

全部评论

相关推荐

醉蟀:你不干有的是人干
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务