class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        int sum = 0,flag = 1;
        vector<int> ans(2,0);
        for(int i = 0;i < nums.size();i++)
            sum ^= nums[i];
        while(!(sum & flag))
            flag <<= 1;

        for(int i = 0;i < nums.size();i++)
        {
            if(flag & nums[i])
                ans[0] ^= nums[i];
            else
                ans[1] ^= nums[i];
        }

        return ans;
    }
};

运行效率:

Runtime: 4 ms, faster than 99.21% of C++ online submissions for Single Number III.
Memory Usage: 10.1 MB, less than 37.50% of C++ online submissions for Single Number III.