❤️‍🔥 Python3 💯 JavaScript 🔥 C++ Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nFirstly, we need to convert decimal number to binary number. \nSecondly, we will change
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirstly, we need to convert decimal number to binary number. \nSecondly, we will change `0` to `1` and `1` to `0`.\nLastly, we will convert this number to decimal number and return it.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst, I thought about it and realized that I have to write code to convert decimal to binary and vice versa.\n\nThen, I thought about built-in functions and there were!\n\nThe `bin(int)` function takes decimal number and returns binary version of it starting with `0b`\nThe `int(bin, 2)` function takes binary string from `bin()` function and converts it to decimal\n\n# Complexity\n- Time complexity: `O(log n)`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of this solution is O(log n) where n is the input number. This is because we are converting the input number to its binary representation, which requires log n operations. Additionally, we are performing string manipulation operations on the binary representation, which also take O(log n) time.\n\n- Space complexity: `O(log n)`\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of this solution is O(log n) as well. This is because we are storing the binary representation of the input number as a string, which requires O(log n) space. Additionally, we are creating a new string to store the complement of the binary representation, which also takes O(log n) space.\n\n# Code\n```python3 []\nclass Solution:\n    def findComplement(self, num: int) -> int:\n        num = bin(num)\n        num = num[2:]\n        num = num.replace(\'1\', \' \')\n        num = num.replace(\'0\', \'1\')\n        num = num.replace(\' \', \'0\')\n        num = \'0b\' + num\n        return int(num, 2)\n\n```\n```JavaScript []\n/**\n * @param {number} num\n * @return {number}\n */\nvar findComplement = function(num) {\n    // Convert the number to a binary string\n    let binaryStr = num.toString(2);\n    let complementStr = \'\';\n\n    // Flip the bits: \'1\' to \'0\' and \'0\' to \'1\'\n    for (let char of binaryStr) {\n        complementStr += (char === \'1\') ? \'0\' : \'1\';\n    }\n\n    // Convert the complemented binary string back to a decimal integer\n    return parseInt(complementStr, 2);\n};\n```\n```C++ []\nclass Solution {\npublic:\n    int findComplement(int num) {\n        // Create a mask with all bits set to 1 where the original number has bits\n        int mask = 0;\n        int temp = num;\n\n        // Create the mask by shifting 1 left and ORing with mask until temp is 0\n        while (temp > 0) {\n            mask = (mask << 1) | 1;\n            temp >>= 1;\n        }\n\n        // XOR the number with the mask to get the complement\n        return num ^ mask;\n    }\n};\n```\n\n
['C++', 'Python3', 'JavaScript']