Created
November 12, 2020 16:01
-
-
Save lishunan246/5ea85a1a411290606adbd9405b917b30 to your computer and use it in GitHub Desktop.
剑指 Offer 56 - I. 数组中数字出现的次数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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