知之者 不如好之者, 好之者 不如樂之者

기계처럼 살지 말고, 즐기는 인간이 되자

Code/LeetCode

[LeetCode] 191. Number of 1 Bits (Easy/Python)

코방코 2023. 3. 2. 06:24
728x90
 

Number of 1 Bits - LeetCode

Can you solve this real interview question? Number of 1 Bits - Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight [http://en.wikipedia.org/wiki/Hamming_w

leetcode.com

Above is the link to the problem.

 

Problem

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

 

Example

Input: n = 00000000000000000000000000001011

Output: 3

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

 

Code

 

This code gets the 10.73% of time complexity and uses 13.9MB of memory.

 

 

728x90
반응형