ice rabbit programming

[LeetCode] 191. Numbers of 1 Bits 본문

PS/LeetCode

[LeetCode] 191. Numbers of 1 Bits

판교토끼 2020. 12. 8. 23:34

 

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인 비트의 개수를 구하는 문제이다. 파이썬으로 풀었더니, 내장 함수에 의해서 쉽게 구현할 수 있었다.

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

'PS > LeetCode' 카테고리의 다른 글

[LeetCode] 231. Power of Two  (0) 2020.12.08
[LeetCode] 217. Contains Duplocate  (0) 2020.12.08
[LeetCode] 190. Reverse Bits  (0) 2020.12.08
[LeetCode] 389. Find the Difference  (0) 2020.12.08
[LeetCode] 448. Find All Numbers Disappeared in an Array  (0) 2020.09.26