본문 바로가기
LeetCode/Top Interview Q. - Easy

LeetCode.Number of 1 Bits [Bit]

by 벤진[Benzene] 2020. 6. 17.

Problem:

Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).

 

Example 1:

Input: 00000000000000000000000000001011

Output: 3

Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.

 

Example 2:

Input: 00000000000000000000000010000000

Output: 1

Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.

 

Example 3:

Input: 11111111111111111111111111111101

Output: 31

Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.

 

-Summary-

Using bin() and count() method of python

1. bin() return the binary of the input number in string format.

2. count 1 and return the number of counts.

 

https://www.programiz.com/python-programming/methods/built-in/bin

 

Python bin()

The syntax of bin() method is: bin(num) bin() Parameters The bin() method takes a single parameter: num - an integer number whose binary equivalent is to be calculated. If not an integer, should implement __index__() method to return an integer. Return val

www.programiz.com

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]

'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글

LeetCode. Missing Number [Bit]  (0) 2020.06.19
LeetCode.Hamming Distance [Bit]  (0) 2020.06.18
LeetCode.Roman to Integer [Math]  (0) 2020.06.16
LeetCode. Count Primes [Math]  (0) 2020.06.15
LeetCode. Count Primes [Math]  (0) 2020.06.14

댓글