릿코드 위클리에 나온 문제라던데 아는 분이 물어봐서 올림class Solution: def makeTheIntegerZero(self, num1: int, num2: int) -> int: cnt = 0 while True: cnt, num1 = cnt + 1, num1 - num2 if num1 <= 0 or cnt > 32: return -1 bits, num = 0, num1 while num: num, bits = num & (num - 1), bits + 1 if bits <= cnt <= num1: return cnt사용된 알고리즘은 비트를 계산하는 Kernighan's algorithm. 의외로 자주 쓰인다.위에서는 pythonic하게 한 줄로 처리했음def countBits(n: int): count = 0 while n > 0: n = n & (n-1) # Key idea. 최하위 비트 제거 count = count + 1 return count
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.