## [Leetcode]260. Single Number III(C++)

### 题目描述

Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

### 例子

#### 例子 1

Input: `nums = [1,2,1,3,2,5]` Output:`[3,5]` Explanation:[`5, 3] is also a valid answer.`

#### 例子 2

Input:`nums = [-1,0]` Output:`[-1,0]`

#### 例子 3

Input:`nums = [0,1]` Output:`[1,0]`

### Constraints

• `2 <= nums.length <= 3 * 10^4`
• `-2^31 <= nums[i] <= 2^31 - 1`
• Each integer in `nums` will appear twice, only two integers will appear once.

### 解题思路

``````#include <vector>
class Solution {
public:
std::vector<int> singleNumber(std::vector<int>& nums) {
// xormask = a ^  b
for (int num : nums) {
}

// in bit mask, the right most different bit beween a and b is set to 1,
// others are set to 0

std::vector<int> ans(2, 0);
for (int num : nums) {
if ((num & bitmask) == 0) {
ans[0] ^= num;
} else {
ans[1] ^= num;
}
}

return ans;
}
};
``````
• 时间复杂度: `O(n)`
• 空间复杂度: `O(1)`

GitHub 代码同步地址： 260.SingleNumberIii.cpp

Built with Hugo
Theme Stack designed by Jimmy