diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/set_mismatch.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/set_mismatch.cpp')
-rw-r--r-- | other/set_mismatch.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/other/set_mismatch.cpp b/other/set_mismatch.cpp new file mode 100644 index 0000000..8c216bc --- /dev/null +++ b/other/set_mismatch.cpp @@ -0,0 +1,19 @@ +#include "leetcode.h" + +class Solution { +public: + vector<int> findErrorNums(vector<int> &nums) { + int p = 0, ac1 = 0, ac2 = 0; + for (uint i = 0; i < nums.size(); ++i) + p ^= (i + 1) ^ nums[i]; + p &= -p; + for (uint i = 0; i < nums.size(); ++i) { + ((nums[i] & p) == 0) ? ac1 ^= nums[i] : ac2 ^= nums[i]; + (((i + 1) & p) == 0) ? ac1 ^= i + 1 : ac2 ^= i + 1; + } + for (auto n : nums) + if (n == ac1) + return {ac1, ac2}; + return {ac2, ac1}; + } +}; |