From d01b45c5cd41e94143f2ad5e564e74adfe6593d0 Mon Sep 17 00:00:00 2001 From: juan Date: Fri, 22 Jul 2022 16:03:37 +0800 Subject: [PATCH] vault backup: 2022-07-22 16:03:37 --- OJ notes/pages/Leetcode Single-Number.md | 27 +++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/OJ notes/pages/Leetcode Single-Number.md b/OJ notes/pages/Leetcode Single-Number.md index 4a04870..4914344 100644 --- a/OJ notes/pages/Leetcode Single-Number.md +++ b/OJ notes/pages/Leetcode Single-Number.md @@ -22,7 +22,8 @@ tag:#bit_manipulation ##### Links: - [Link to problem](https://leetcode.com/problems/single-number/) -- [Learn about XOR]() +- [Learn about XOR](https://en.wikipedia.org/wiki/Exclusive_or#Computer_science) +- [Explanation](https://leetcode.com/problems/single-number/discuss/1772139/C%2B%2Bor-Explained-Everything-w-WHY-XOR-WORKSor-BRUTE-FORCE-TO-OPTIMIZEDor-STEP-BY-STEP-DRY-RUN) ___ ### Problem @@ -59,5 +60,29 @@ You must implement a solution with a linear runtime complexity and use only co > This is a #bit_manipulation problem utilizing the XOR operator +The XOR operator has some properties: + +``` +a ^ a = 0; +a ^ b ^ a = a ^ a ^ b = b; +``` + +This can be used for our problem. ### Solution + +```cpp +class Solution { +public: + int singleNumber(vector &nums) { + // Using XOR operator + int ans = 0; + + for (int i : nums) { + ans ^= i; + } + + return ans; + } +}; +``` \ No newline at end of file