본문 바로가기
python-algorithm

leetcode 1009. Complement of Base 10 Integer

by 무적김두칠 2023. 3. 20.

https://leetcode.com/problems/complement-of-base-10-integer/description/

 

Complement of Base 10 Integer - LeetCode

Can you solve this real interview question? Complement of Base 10 Integer - 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 b

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def bitwiseComplement(self, n: int-> int:
        s = bin(n)[2:]
        answer = ''
        for i in s:
            if i == '1':
                answer += '0'
            else:
                answer += '1'
        answer = int(answer, 2)
        return answer
cs
반응형

댓글