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
'Leetcode' 카테고리의 다른 글
[LeetCode] Max Consecutive Ones 파이썬 (문자열 압축) (0) | 2021.04.29 |
---|---|
[LeetCode] License Key Formatting 파이썬 (str.replace) (0) | 2021.04.29 |
[LeetCode] Island Perimeter 파이썬 (island, traverse) (0) | 2021.04.19 |
[LeetCode] Hamming Distance 파이썬 (bitwise) (0) | 2021.04.19 |
[LeetCode] Repeated Substring Pattern 파이썬 (KMP) (0) | 2021.04.18 |
댓글