剑指OFFER----面试题56 - I. 数组中数字出现的次数

链接:https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/

代码

class Solution {
public:
    vector<int> singleNumbers(vector<int>& nums) {
        int sum = 0;
        for (auto x: nums) sum ^= x;
        int k = 0;
        while (!(sum >> k & 1)) ++k;
        int first = 0;
        for (auto x: nums) {
            if (x >> k & 1) first ^= x;
        }
        return vector<int>{sum ^ first, first};
    }
};
posted @ 2020-03-11 14:08  景云ⁿ  阅读(42)  评论(0编辑  收藏  举报