191. Number of 1 Bits Leetcode Python

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

For example, the 32-bit integer ’11‘ has binary representation 00000000000000000000000000001011,
so the function should return 3.

Credits:

Special thanks to @ts for adding this problem and creating all test cases.

This problem can be done in log(n) time, need >> operations..

class Solution:
    # @param n, an integer
    # @return an integer
    def hammingWeight(self, n):
        res=0
        while n>0:
            res+=n&1
            n>>=1
        return res

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-11-06 16:21:57

191. Number of 1 Bits Leetcode Python的相关文章

338. Counting Bits && 191. Number of 1 Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up: It is very e

LeetCode 191. Number of 1 bits (位1的数量)

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should retu

【LeetCode】191 - Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

Java for LeetCode 191 Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

LeetCode 191 Number of 1 Bits(1 比特的数字们)

翻译 写一个函数获取一个无符号整型数,并且返回它的"1"比特的数目(也被叫做Hamming weight). 例如,一个32位整型数"11",转换成二进制是00000000000000000000000000001011,所以这个函数应该返回3. 原文 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the

191. Number of 1 Bits(LeetCode)

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should retu

【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)

题目 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should r

(easy)LeetCode 191.Number of 1 Bits

Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the fun

[LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)

描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight). 输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数. 解析 消除最后的1 观察一下 n 与 n-1 这两个数的二进制表示:对于 n-1 这个数的二进制来说,相对于 n 的二进制,它的最末位的一个 1 会变成 0,最末位一个 1