LeetCode 191. Number of 1 Bits

개발공부를해보자·2025년 3월 3일

LeetCode

목록 보기
74/95

파이썬 알고리즘 인터뷰 74번(리트코드 191번) Number of 1 Bits
https://leetcode.com/problems/number-of-1-bits/

나의 풀이

class Solution:
    def hammingWeight(self, n: int) -> int:
        result = 0
        while n > 0:
            if n & 1:
                result += 1
            n >>= 1
        return result

다른 풀이

class Solution:
    def hammingWeight(self, n: int) -> int:
        return bin(n).count('1')
profile
개발 공부하는 30대 비전공자 직장인

0개의 댓글