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
| class Solution { public: vector<int> findErrorNums(vector<int>& nums) { int n = nums.size(), mask = 0, i = 0; while (++i <= n) mask ^= i; for (int num : nums) mask ^= num; i = 0, mask = mask & (-mask); int num1 = 0, num2 = 0; while (++i <= n) { if ((i & mask) == 0) num1 ^= i; else num2 ^= i; } for (int num : nums) { if ((num & mask) == 0) num1 ^= num; else num2 ^= num; } for (int num : nums) { if (num == num1) return {num1, num2}; if (num == num2) return {num2, num1}; } return {}; } };
|