476: Solution with step by step explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Convert the given integer \'num\' to its binary string representation 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Convert the given integer \'num\' to its binary string representation using the built-in \'bin()\' function in Python.\n2. Flip all bits in the binary string obtained in step 1 to obtain the binary string representation of its complement. This can be done by iterating over each bit in the binary string and replacing each \'0\' with \'1\' and vice versa.\n3. Convert the binary string obtained in step 2 back to its decimal integer representation using the built-in \'int()\' function in Python with base 2.\n4. Return the integer obtained in step 3 as the result.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def findComplement(self, num: int) -> int:\n        # Convert num to binary string\n        binary = bin(num)[2:]\n\n        # Flip all bits in the binary string\n        flipped_binary = \'\'.join([\'0\' if bit == \'1\' else \'1\' for bit in binary])\n\n        # Convert the flipped binary string back to an integer\n        if flipped_binary:\n            return int(flipped_binary, 2)\n        else:\n            return 1\n\n```
['Bit Manipulation', 'Python', 'Python3']