leetcode number of 1 bits python

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 return 3.

python代码:

class Solution:
# @param n, an integer
# @return an integer
  def hammingWeight(self, n):
    count=0
    if n==0:        #输入为0时,输出为0
      return count
    count+=1
    while n&n-1:      #利用位运算,获得1的个数
      n=n&n-1
      count=count+1
    return count

时间: 2024-10-22 23:36:51

leetcode number of 1 bits python的相关文章

2016.5.16——leetcode:Number of 1 Bits ,

leetcode:Number of 1 Bits 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n-1)[n = n & (n-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 exam

[LeetCode] 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]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 Number of 1 Bits 计算1的个数

题意:提供一个无符号32位整型uint32_t变量,返回其二进制形式的1的个数. 思路:取出一位,就右移1位,挤掉它,循环32次,逐个判断.没难度就不解释了,可能有更好解法,等待第2次思考. 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) { 4 if(!n) return 0; 5 if(n==1) return 1; 6 7 uint32_t MASK = 1, temp=n; 8 int i, cnt = 0; 9

[Leetcode] 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] 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——Number of 1 Bits

public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_count = 0; for(int i=0; i<b_str.length(); i++) { if(b_str.charAt(i) == '1') { b_count ++; } } return b_count; }

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 retu

【leetcode】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