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 return 3.
1 2 3 4 5 6 7 8 9 10 11 12 | 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 |
1 2 3 4 5 6 7 8 9 10 11 12 | 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 |
1 2 3 4 5 6 7 8 | 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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」