public class Solution {
// 利用公式 n & n-1,每计算一次减去一次1
public int hammingWeight(int n) {
int count = 0;
while(n != 0){
n = n & (n-1);
count ++;
}
return count;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
//n &(n - 1)可以消除最右边的一个1
int sum = 0;
while(n != 0){
sum ++;
n = n & (n - 1);
}
return sum;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int cnt = 0;
while (n != 0) {
n -= (n & -n);
cnt++;
}
return cnt;
}
}
func hammingWeight(num uint32) int {
//将2^i和num进行与运算,当且仅当n的第i为不为0时,运算结果才不会0
res := 0
for i :=0;i < 32;i++{
if (1 << i & num) > 0{ //2^i 计算方式为1 << i,与运算为&
res++
}
}
return res
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
//位运算公式的应用 n& (n-1) 得到的结果是去掉最右侧的1
//比如n=1001 相与之后就是1000 ,1010 相与之后就是1000
//所以我们只要在所有的1被与完之前统计与操作的次数就行
int count = 0;
while (n != 0) {
n = n & (n - 1);
count++;
}
return count;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while (n != 0) {
// 把n最右边的1变成0
n &= (n - 1);
res ++;
}
return res;
}
}
public class Solution {
//方法一:使用 >>> 1 逐位判断
//时间复杂度 O(logN)
//空间复杂度 O(1):变量res使用常数大小额外空间。
public int hammingWeight1(int n) {
int i = 0;
while (n != 0) {
if ((n & 1) == 1)
i++;
n >>>= 1;
}
return i;
}
//方法二:巧用 n & n - 1
//时间复杂度 O(M) :M为二进制数字n中1的个数,则需循环M次(每轮消去一个1 ),占用 O(M)O(M)O(M) 。
//空间复杂度 O(1):变量res使用常数大小额外空间。
public int hammingWeight2(int n) {
int i = 0;
while (n != 0) {
n &= n - 1;
i++;
}
return i;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n != 0){
n = n & (n-1);
count++;
}
return count;
}
}
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
//时间复杂度为O(n)
//空间复杂度为O(1)
int count=0;
while (n!=0){
//n&(n-1)结果是把n的二进制中的最低位的1变为0;
//每次执行你&(n-1)表示不断的把n最低位的1转为0 ,计算一下次数就是1的个数
n=n&(n-1);
count++;
}
return count;
}
class Solution:
def hammingWeight(self, n: int) -> int:
num = 0
while n:
if n&1==1: num+=1
n = n>>1
return num
空间O(logn)
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while(n != 0) {
res ++;
n = n & (n-1);
}
return res;
}
}
public class Solution {
// you need to treat n as an unsigned value
//两种方法:1.除以2.奇数
public int hammingWeight(int n) {
int count = 0;
while(n!=0){
if(n%2==1){
count++;
}
n = n/2;
}
return count;
}
//位运算法
public int hammingWeight(int n){
int count = 0;
while(n!=0){
n = n&(n-1);
count++;
}
return count;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while(n != 0) {
res ++;
n = n & (n-1);
}
return res;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int sum = 0;
while(n != 0) {
sum++;
//不断让当前的 n 与 n−1 做与运算,直到 n 变为 0 即可
n = n&(n-1);
}
return sum;
}
}
public class Solution {
//一个二进制数与这个数减一做与运算,可以消掉右边数的第一个一
//时间复杂度O(logn)
//空间复杂度O(1)
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int sum = 0;
while(n != 0){
n = n & (n - 1);
sum ++;
}
return sum;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while (n != 0){
// 只有最后一位为1的时候n&1=1
res += n & 1;
//逐位判断
n >>>= 1;
}
return res;
}
public int hammingWeight(int n) {
//(n -1) 最右边的1变为0 该数的右边变为1 省略了中间0 的判断
int res = 0;
while(n != 0) {
res++;
//a & b 同1才1
n &= n - 1;
}
return res;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
//返回n的二进制中1的位数
// return Integer.bitCount(n);
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int sum = 0;
while(n!=0){
n = n&(n-1);
sum++;
}
return sum;
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while(n != 0) {
res += n & 1;
n >>>= 1;
}
return res;
}
}
“`java
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int sum = 0;
while (n != 0) {
n = n & (n – 1);
sum++;
}
return sum;
}
}
“`时间:O(log(n)),空间:O(1)
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
for(int i = 0;i < 32;i++){
//和32位上的每一位1相与,计算1的个数
if((n & (1 << i)) != 0) count++;
}
return count;
}
}
位运算优化
例如 6&(6-1) = 4
6 = (110) 4 = (100) n & (n-1)后能去除最后一位1
时间复杂度O(logn)循环次数等于二进制中1的个数,最坏情况下全部为1,循环logn次
空间复杂度O(1)
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n != 0){
n &= n-1;
count++;
}
return count;
}
}
/**
脑残写法,为什么不用循环比对呢
时间复杂度O(n)
空间复杂度O(1)
*/
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ans = 0;
if(n < 0){
n = n ^ (0b11111111111111111111111111111111);
ans = -32;
}
while(n != 0){
if((n&1) == 1){
ans++;
}
n = n >> 1;
}
return Math.abs(ans);
}
}
/**
时间复杂度O(logn)
空间复杂度O(1)
*/
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ans = 0;
while(n != 0){
n = n & (n - 1);
ans++;
}
return ans;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int k = 0;
while(n != 0){
n = n & (n-1);
k++;
}
return k;
}
}
共以下 36 个回答
记住n = n & (n – 1);可以消除最右侧的1
lowbit 取最低位1
时间O(log2N) 空间O(1)
时间复杂度:O(logn)
空间复杂度:O(1)
时间复杂度:O(logN)
空间复杂度:O(1)
时间复杂度:O(1)
时间O(n),空间O(1)
时间:O(n)
空间:O(1)
现学现卖,哈哈哈哈,用位运算去统计 空间O(n)
空间O(logn)
时间O(1)
技巧n&(n-1)
时间O(logn)
空间O(1)
逐位判断
时间复杂度为O(m) m为1的个数
空间复杂度为O(1)
重点理解为n&n-1
//统计位数
class Solution {
public:
int hammingWeight(uint32_t n) {
int res=0;
for(int i=0;i<32;++i){
int ret=n&(1<<i);
if(ret) res++;
}
return res;
}
};
每进行一次n & n-1与操作就会使数的最后一位的1变成0
时间复杂度:O(logn),空间复杂度O(1)
“`java
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int sum = 0;
while (n != 0) {
n = n & (n – 1);
sum++;
}
return sum;
}
}
“`时间:O(log(n)),空间:O(1)
循环检查二进制位
时间复杂度O(k)其中k是int型的二进制位数,k = 32
空间复杂度O(1)
位运算优化
例如 6&(6-1) = 4
6 = (110) 4 = (100) n & (n-1)后能去除最后一位1
时间复杂度O(logn)循环次数等于二进制中1的个数,最坏情况下全部为1,循环logn次
空间复杂度O(1)
这道题很简单,移位加与运算统计个数就行
时间复杂度 O(logn)
空间复杂度O(1)
时间复杂度 O(n)
时间复杂度为O(lgn),空间复杂度为O(1)
转为二进制的字符串遍历,时间复杂度on