LeetCode 1009: Binary Complement of Integer

LeetCode Problem 1009: "Complement of base 10": The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation. For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2. Given an integer n, return its complement. Approach: First get the binary string representation of given number using bin() and str() functions, second iterate through the string and append '0' to a new string variable if you get '1' and '1' if you get '0', last return the integer value of the new string variable using the int() function with base 2. #Python #Strings #LeetCode #CompetitiveProgramming #DataStructures #DSA #Algorithms #Programming #ProblemSolving

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories