본문 바로가기

두두의 알고리즘/문제

[비트연산] 릿코드 Easy 191 'Number of 1 Bits' (Python)

728x90

<문제 링크>

https://leetcode.com/problems/number-of-1-bits/

 

Number of 1 Bits - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


<문제 풀이>

  1. 정수로 들어온 값을 2진수로 바꿔주고 1의 개수를 센다

<코드>

class Solution:
    def hammingWeight(self, n: int) -> int:
        return str(bin(n)).count('1')