博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 191. Number of 1 Bits
阅读量:5778 次
发布时间:2019-06-18

本文共 2720 字,大约阅读时间需要 9 分钟。

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

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

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

 

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

 

class Solution(object):    def hammingWeight(self, n):        """        :type n: int        :rtype: int        """        assert n>=0        return bin(n).count('1')

 

其他解法:

Another several method of O(1) time.

Add 1 by Tree:

// This is a naive implementation, shown for comparison, and to help in understanding the better functions. // It uses 24 arithmetic operations (shift, add, and).int hammingWeight(uint32_t n) { n = (n & 0x55555555) + (n >> 1 & 0x55555555); // put count of each 2 bits into those 2 bits n = (n & 0x33333333) + (n >> 2 & 0x33333333); // put count of each 4 bits into those 4 bits n = (n & 0x0F0F0F0F) + (n >> 4 & 0x0F0F0F0F); // put count of each 8 bits into those 8 bits n = (n & 0x00FF00FF) + (n >> 8 & 0x00FF00FF); // put count of each 16 bits into those 16 bits n = (n & 0x0000FFFF) + (n >> 16 & 0x0000FFFF); // put count of each 32 bits into those 32 bits return n; } // This uses fewer arithmetic operations than any other known implementation on machines with slow multiplication. // It uses 17 arithmetic operations. int hammingWeight(uint32_t n) { n -= (n >> 1) & 0x55555555; //put count of each 2 bits into those 2 bits n = (n & 0x33333333) + (n >> 2 & 0x33333333); //put count of each 4 bits into those 4 bits n = (n + (n >> 4)) & 0x0F0F0F0F; //put count of each 8 bits into those 8 bits n += n >> 8; // put count of each 16 bits into those 8 bits n += n >> 16; // put count of each 32 bits into those 8 bits return n & 0xFF; } // This uses fewer arithmetic operations than any other known implementation on machines with fast multiplication. // It uses 12 arithmetic operations, one of which is a multiply. int hammingWeight(uint32_t n) { n -= (n >> 1) & 0x55555555; // put count of each 2 bits into those 2 bits n = (n & 0x33333333) + (n >> 2 & 0x33333333); // put count of each 4 bits into those 4 bits n = (n + (n >> 4)) & 0x0F0F0F0F; // put count of each 8 bits into those 8 bits return n * 0x01010101 >> 24; // returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) }

——From Wikipedia.

转载地址:http://gduyx.baihongyu.com/

你可能感兴趣的文章
去云端的多条途径
查看>>
Docker容器从一知半解到入门
查看>>
关于“方法参数”
查看>>
Redis命令总结
查看>>
unable to write 'random state'错误解决
查看>>
context:annotation-config vs component-scan
查看>>
结构体和类的内存对齐原则-这一次弄清楚了对齐的本质规则
查看>>
Centos编译安装Nginx和PHP
查看>>
Linux-grep命令
查看>>
exgcd、二元一次不定方程学习笔记
查看>>
经典sql
查看>>
CSS3边框会动的信封
查看>>
JavaWeb实例设计思路(订单管理系统)
查看>>
source insight中的快捷键总结
查看>>
PC-IIS因为端口问题报错的解决方法
查看>>
java四种线程池简介,使用
查看>>
一般处理程序(.ashx)中session的使用方法
查看>>
EasyUI笔记(二)Layout布局
查看>>
ios View之间的切换 屏幕旋转
查看>>
typedef BOOL(WINAPI *MYFUNC) (HWND,COLORREF,BYTE,DWORD);语句的理解
查看>>