Skip to content

Instantly share code, notes, and snippets.

@lishunan246
Created November 12, 2020 16:01
Show Gist options
  • Save lishunan246/5ea85a1a411290606adbd9405b917b30 to your computer and use it in GitHub Desktop.
Save lishunan246/5ea85a1a411290606adbd9405b917b30 to your computer and use it in GitHub Desktop.
剑指 Offer 56 - I. 数组中数字出现的次数
int lowbit(int x) {
return x & (-x);
}
class Solution {
public:
vector<int> singleNumbers(vector<int>& nums) {
int x = 0;
for (auto n : nums) {
x ^= n;
}
auto lb = lowbit(x);
int a = 0;
int b = 0;
for (auto n : nums) {
if (n & lb) {
a ^= n;
}
else {
b ^= n;
}
}
return {a, b};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment