zoukankan      html  css  js  c++  java
  • 二进制中位为1的个数

    一、题目描述

     输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)

     示例 1:

      输入:00000000000000000000000000001011

      输出:3

      解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'

     示例 2:

      输入:00000000000000000000000010000000

      输出:1

      解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 '1'

     示例 3:

      输入:11111111111111111111111111111101

      输出:31

      解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'

    class Solution(object):
        def hammingWeight(self, n):
            """
            :type n: int
            :rtype: int
            """
            
            num = 0
            while n:
                num += 1
                n &= n-1
            return num
            
    

      

  • 相关阅读:
    php多态
    ssl certificate problem: self signed certificate in certificate chain
    test plugin
    open specific port on ubuntu
    junit vs testng
    jersey rest service
    toast master
    use curl to test java webservice
    update folder access
    elk
  • 原文地址:https://www.cnblogs.com/always-fight/p/10361125.html
Copyright © 2011-2022 走看看