#-*- coding: UTF-8 -*-
class Solution(object):
    def hammingWeight(self, n):
        if n<=0:return n
        mid=[]
        while True:
            if n==0:break
            n,mod=divmod(n,2)
            mid.append(mod)
        mid.reverse()
        return mid.count(1)

sol=Solution()
print sol.hammingWeight(11)

【leetcode❤python】191. Number of 1 Bits的更多相关文章

  1. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  2. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  3. 【一天一道LeetCode】#191. Number of 1 Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  4. 【LeetCode】191. Number of 1 Bits

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  5. 【刷题-LeetCode】191 Number of 1 Bits

    Number of 1 Bits Write a function that takes an unsigned integer and return the number of '1' bits i ...

  6. 【leetcode❤python】 374. Guess Number Higher or Lower

    #-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...

  7. 【leetcode❤python】 414. Third Maximum Number

    #-*- coding: UTF-8 -*- #l1 = ['1','3','2','3','2','1','1']#l2 = sorted(sorted(set(l1),key=l1.index,r ...

  8. 【leetcode❤python】 9. Palindrome Number

    #回文数#Method1:将整数转置和原数比较,一样就是回文数:负数不是回文数#这里反转整数时不需要考虑溢出,但不代表如果是C/C++等语言也不需要考虑class Solution(object):  ...

  9. 【leetcode❤python】Convert a Number to Hexadecimal

    #-*- coding: UTF-8 -*- class Solution(object):    hexDic={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6', ...

随机推荐

  1. 转:V$SQL,V$SQLAREA,V$SQLTEXT

    V$SQL*表用于查看Shared SQL Area中SQL情况 V$SQLTEXT V$SQLTEXT用途很简单,就是用来查看完整的SQL语句,V$SQL和V$SQLAREA只能显示1000 byt ...

  2. EBS fnd_global.apps_initialize

    原型:fnd_global.apps_initialize(user_ID,                                             Responsibility_id ...

  3. 在linux中的virtualbox无法挂载usb设备的解决方法

    方法来源于网络. 在安装完virtualbox之后,virtualbox会建立一个名为 vboxusers 的组,将你的用户名加入到该组即可. 命令参考: #usermod -a -G vboxuse ...

  4. 使用Graham扫描法求二维凸包的一个程序

    #include <iostream> #include <cstring> #include <cstdlib> #include <cmath> # ...

  5. Openstack的删除错误网桥,虚拟网络

    在实验openstack的各种网络模式时,可能会产生一些错误的网络指向,需要删除那些网桥. 执行前 [root@node-9 ~]# ifconfig br40 Link encap:Ethernet ...

  6. ES6,ES2105核心功能一览,js新特性详解

    ES6,ES2105核心功能一览,js新特性详解 过去几年 JavaScript 发生了很大的变化.ES6(ECMAScript 6.ES2105)是 JavaScript 语言的新标准,2015 年 ...

  7. ansible

    3.1 配置 #vim /etc/ansible/hosts       //定义主机,支持IP和域名,支持分组 [local] 127.0.0.1 [nginx] 192.168.0.10     ...

  8. Java常见编码方式简介

    计算机只能识别二进制数据,早期由来是电信号. 为了方便应用计算机,让它可以识别各个国家的文字. 就将各个国家的文字用数字来表示,并一一对应,形成一张表. ASCII:美国标准信息交换码. 用一个字节的 ...

  9. centos7.0 没有netstat 和 ifconfig命令问题

    yum install wget 运行  yum install net-tools  就OK了 默认CentOS已经安装了OpenSSH,即使你是最小化安装也是如此.所以这里就不介绍OpenSSH的 ...

  10. Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

    docs.jboss.org文档示例代码:(http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/) sta ...