//给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。你可以按 任意顺序 返回答案。 
//
// 
//
// 进阶:你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现? 
//
// 
//
// 示例 1: 
//
// 
//输入:nums = [1,2,1,3,2,5]
//输出:[3,5]
//解释:[5, 3] 也是有效的答案。
// 
//
// 示例 2: 
//
// 
//输入:nums = [-1,0]
//输出:[-1,0]
// 
//
// 示例 3: 
//
// 
//输入:nums = [0,1]
//输出:[1,0]
// 
//
// 提示: 
//
// 
// 2 <= nums.length <= 3 * 104 
// -231 <= nums[i] <= 231 - 1 
// 除两个只出现一次的整数外,nums 中的其他数字都出现两次 
// 
// Related Topics 位运算 
// 👍 365 👎 0

/*
* 260 只出现一次的数字 III
* 2021-03-12 15:01:34
* @author oxygenbytes
*/ 
#include "leetcode.h" 
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        int s = 0;
        // if target is a && b
        for(auto x : nums) s ^= x;
        // now s = a ^ b;
        int k = 0;
        while(!(s >> k & 1)) k++;
        // mark the pos where bit == 1
        // divide by the bit=1
        int s2 = 0;
        for(auto x : nums) if(x >> k & 1) s2 ^= x;
        return {s2, s2 ^ s};
    }
};
//leetcode submit region end(Prohibit modification and deletion)