본문 바로가기
Leetcode

[LeetCode] Number Complement 파이썬 (format, bitwise)

by YGSEO 2021. 4. 19.
728x90

format() 함수를 사용해서 진법을 출력할때 앞의 str를 제거하고 출력할 수 있다.

Format Specification Mini-Language

이런식으로 출력된다.

print(format(num, 'b'))
print(bin(num))

class Solution:
    def findComplement(self, num: int) -> int:

        com_bi = [str(abs(int(x)-1)) for x in str(format(num, 'b'))]
        return com_bi

abs(int(x) - 1) 을 해줌으로써 1 -> 0 , 0 -> 1로 swap 해준다.

728x90

댓글