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 ret = 0;
while(n != 0){
n &= n-1;//将二进制位中最低位的1变为0之后的结果
ret++;
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 count=0;
for(int i=0;i<32;i++){
int temp = 1&n;
if(temp==1) count++;
n = n>>1;
}
return count;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n!=0)
{
if((n&1)1)//预算付优先级
{
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){
n = n & (n-1); //只有真真为真
sum++;
}
return sum;
}
}
public int hammingWeight(int n) {
//输入:n = 11 (控制台输入 00000000000000000000000000001011)
int index = 1;
int count = 0;
for (int i = 0; i < 32; i++) {
if ((index & n) != 0) {
count++;
}
index = index << 1;
}
return count;
}
/**
* @param {number} n - a positive integer
* @return {number}
*/
var hammingWeight = function(n) {
let res = 0;
while(n) {
res += n & 1;
n >>>= 1;
}
return res;
};
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n!=0){
// 每次消除最右边的1
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 count = 0;
while(n != 0){
int lastOne = n & ((~n) + 1);//去二进制的最后一位1
n ^= lastOne;
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 & 1);
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){
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 cnt = 0;
while(n != 0) {
if(n & 1 == 1) {
cnt ++;
}
n >>= 1;
}
return cnt;
}
}
共以下 21 个回答
逐位判断
“`c++
//公式
class Solution {
public:
int hammingWeight(uint32_t n) {
int count=0;
while(n)
{
n=n&(n-1);
count++;
}
return count;
}
};
c++ 简单的位运算
class Solution {
public:
int hammingWeight(uint32_t n) {
int s=0;
while(n)
{
s+=n&1;
n>>=1;
}
return s;
}
};
”’java
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变为0之后的结果
ret++;
}
”’
时间复杂度O(logn)
空间复杂度O(1)
时间复杂度:O(logn)
空间复杂度:O(1)
时间复杂度: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)
{
if((n&1)1)//预算付优先级
{
count+=(n&1);
}
n>>>=1;
}
return count;
}
}
代码
不难
JS简单题
7.24
Day16:下面是代码
以下是代码:
将n往右移来比较,会超时。用另一个数不停左移,与n比较就不会超时。有点奇妙!
时间复杂度 On 空间复杂度 O1