public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ret = 0;
while (n != 0) {
n &= (n-1); // 消除最右边的一个1
ret ++;
}
return ret;
}
}
解题思路
– n & (n-1) 就能不间断消除n二进制右边位的1,全部消除完毕,n=0。消除了多少次就说明有多少个1
【题解1】
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
// n & (n-1) 就能不断消除n二进制右边位的1,全部消除完毕,n=0
// n 100100 ; n-1 100011
// n & (n-1) 10000 ,
int i = 0;
while (n != 0) {
n = 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){
count += n&1;
n >>>= 1;
}
return count;
}
}
/**
* @param {number} n - a positive integer
* @return {number}
*/
var hammingWeight = function (n) {
let cnt = 0
while (n != 0) {
cnt += n & 1
n >>>= 1
}
return cnt
};
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int num=0;
while(n!=0){
num++;
n=n&(n-1);
}
return num;
}
}
时间复杂度:O(n)n为1的位数
空间复杂度:O(1)
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int cnt = 0;
while (n != 0) {
if ((n & 1) == 1) {
cnt++;
}
n >>>= 1;
}
return cnt;
}
}
//利用n&1是,判断1的个数
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
count += (n & 1);
n >>>= 1;
}
return count;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int sum = 0;
while(n != 0){
// 消除最右边的1
n = n & (n - 1);
sum ++;
}
return sum;
}
}
class Solution {
public int hammingDistance(int x, int y) {
return hammingWeight(x ^ y);
}
public int hammingWeight(int n) {
int sum = 0;
while (n != 0) {
// 消除最右边的1
n = n & (n - 1);
sum++;
}
return sum;
}
}
n&n-1会将二进制最低位的1变为0,变了就相当于有一个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&(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){
if((n & 1) 1){
res++;
}
n >>>= 1;
}
return res;
}
}
时间:O(n)
空间:O(1)
public class Solution {
/**
* 使用位运算,计算出一个数对应的二进制中 1 的个数
* @param n
* @return
*/
public int hammingWeight1(int n) {
// n 转换为二进制,从后向前记录
int res = 0;
// 如果二进制的最后一位是 1 的话,res++,n 无符号右移(因为负数右移,高位补 1)
while (n != 0) {
if ((n & 1) == 1) {
res++;
}
n = n >>> 1;
}
return res;
}
public int hammingWeight(int n) {
// n & (n - 1),相当于消除 n 的二进制的最后面的 1,不断的消除,直至 n == 0 为止
int res = 0;
while (n != 0) {
n = n & (n - 1);
res++;
}
return res;
}
}
共以下 13 个回答
时间复杂度O(logN),空间复杂度O(1)
解题思路
– n & (n-1) 就能不间断消除n二进制右边位的1,全部消除完毕,n=0。消除了多少次就说明有多少个1
【题解1】
思路: 使用 位运算 若n最后一位为1 则 n&1 1 否则 n&1 0
思路:根据&1的特性,从最右边的位逐步比较
每次运算消去n中最后一位的1。
时间复杂度O(N),空间复杂度O(1)。
时间复杂度O(logn)
空间复杂度O(1)
//利用n&1是,判断1的个数
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
count += (n & 1);
n >>>= 1;
}
return count;
}
}
LCR 133. 位 1 的个数 – 力扣(LeetCode)
另一道类似的题:461. 汉明距离 – 力扣(LeetCode)
先异或再算有几个1,异或(无进位的加法)
Ologn o1
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while(n != 0){
if((n & 1) 1){
res++;
}
n >>>= 1;
}
return res;
}
}
时间:O(n)
空间:O(1)
思路:位运算,逐位遍历;n & (n – 1)。
时间复杂度:O(logn);O(M),M 为二进制数 n 中 1 的个数。
空间复杂度:O(1)