1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <vector> using std::vector; class Solution { public: vector<int> singleNumber(vector<int> &nums) { int diff = 0; for (auto i : nums) { diff ^= i; } int mask = diff & (-diff); int result = 0; for (auto i : nums) { result ^= (i & mask) ? i : 0; } return {result, result ^ diff}; } };