Programming/leetcode

191. Number of 1 Bits

홍열 2021. 2. 1. 17:19
728x90

2월 1일 문제

십진수를 이진수로 변환했을때 1비트가 몇개 있느냐 검색하는 문제 

 

난이도는 쉽다.

 

    def hammingWeight(self, n: int) -> int:
        count = 0
        while n >0:
            if n & 1 == 1:
                count += 1
            n>>=1
        return count
    def hammingWeight(self, n: int) -> int:
        count = 0
        print(n)
        while n >0:
            count += n%2
            n//= 2
        return count