剑指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};
}
};