首页 > 试题广场 >

二进制中1的个数

[编程题]二进制中1的个数
  • 热度指数:854588 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
输入一个整数 n ,输出该数32位二进制表示中1的个数。其中负数用补码表示。

数据范围:
即范围为:
示例1

输入

10

输出

2

说明

十进制中10的32位二进制表示为0000 0000 0000 0000 0000 0000 0000 1010,其中有两个1。       
示例2

输入

-1

输出

32

说明

负数使用补码表示 ,-1的32位二进制表示为1111 1111 1111 1111 1111 1111 1111 1111,其中32个1    
推荐
public class Solution {
    //从n的2进制形式的最右边开始判断是不是1
    /*
    * 该解法如果输入时负数会陷入死循环,
    * 因为负数右移时,在最高位补得是1
    * 二本题最终目的是求1的个数,那么会有无数个
    * 1了。
    */
    //-------------可能陷入死循环的解法---------------------
    public static int NumberOf1_CanNotUse(int n) {
        int count = 0;
        while (n != 0) {
            /*
            * 用1和n进行位与运算,
            * 结果要是为1则n的2进制形式
            * 最右边那位肯定是1,否则为0
            */
            if ((n & 1) == 1) {
                count++;
            }
            //把n的2进制形式往右推一位
            n = n >> 1;
        }
        return count;
    }
    //---------------正解--------------------------------
    //思想:用1(1自身左移运算,其实后来就不是1了)和n的每位进行位与,来判断1的个数
    private static int NumberOf1_low(int n) {
        int count = 0;
        int flag = 1;
        while (flag != 0) {
            if ((n & flag) != 0) {
                count++;
            }
            flag = flag << 1;
        }
        return count;
    }
    //--------------------最优解----------------------------
    public static int NumberOf1(int n) {
        int count = 0;
        while (n != 0) {
            ++count;
            n = (n - 1) & n;
        }
        return count;
    }
    public static void main(String[] args) {
        //使用n=10,二进制形式为1010,则1的个数为2;
        int n = -10;
        System.out.println(n + "的二进制中1的个数:" + NumberOf1(n));
    }
}

编辑于 2015-08-18 23:23:00 回复(148)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return int整型
     */
    public int NumberOf1 (int n) {
        // write code here
        int sum=0;
        while(n!=0) {
            sum+=(n&1);
            n = n>>>1;
        }
        return sum;
    }

发表于 2024-01-17 23:19:43 回复(0)
import java.util.*;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param n int整型
     * @return int整型
     */
    public int NumberOf1 (int n) {
        // write code here
        int count=0;
        while(n!=0){
            n=n&(n-1);
            count++;
        }
        return count;
    }
}
发表于 2023-10-29 19:14:17 回复(0)
public class Solution {
    public int NumberOf1(int n) {
        int ans=0;
        while(n!=0){
            ans+=n&1;//判断n的最右位是不是1
            n>>>=1;  //把n向右移动一位
        }
        return ans;

    }
}

发表于 2023-05-25 19:57:24 回复(0)
public static int NumberOf1(int n) { int count = 0; for (int i = 0; i < 32; i++) { if( (n & 1) == 1){
            count++;
        }
        n = n>>1;
    } return count;
}
发表于 2023-03-30 14:27:41 回复(0)
 int count = 0;  for (int i = 0; i < 32; i++) { if ((n & (1 << i)) != 0) {
            count++;  }
} return count; }
1<<i 是将1左移i位,即第i位为1,其余位为0  例如1<<2 0001->0100   n&(1<<i)是将左移i位的1n进行按位与,即为保留n的第i位,其余位置零   如果ni位为0,则n&(1<<i)的值为0 否则不为0 常用if(n&(1<<i)==0)用于判断n的第i位是否为0

发表于 2023-03-09 11:04:53 回复(0)
public class Solution {
    public int NumberOf1(int n) {
        int[] num = new int[32];
        int count = 0;
        if (n >= 0) {
            for (int i = 0; i < 32; i++) {
                num[31 - i] = n % 2;
                n = n / 2;
            }
        } else {
            for (int i = 0; i < 32; i++) {
                num[31 - i] = n % 2;
                n = n / 2;
            }
            for (int i = 0; i < num.length; i++) {
                if (num[i] == 0)num[i] = 1;
                else num[i] = 0;
            }
            if (num[num.length - 1] == 0) {
                num[num.length - 1] = 1;
            } else {
                for (int i = num.length - 1; i >= 0; i--) {
                    if (num[i] == 1) {
                        num[i] = 0;
                    } else {
                        num[i] = 1;
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < num.length; i++) {
            if (num[i] == 1)count++;
        }
        return count;
    }
}

发表于 2022-11-03 15:27:09 回复(0)

public class Solution {
    public int NumberOf1(int n) {
        return Integer.bitCount(n);
    }
}

发表于 2022-09-16 16:39:52 回复(0)
import java.util.*;
public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        for(int i = 31; i >=0; i--){
            if((n & 1 << i) != 0){
                count++;
            }
        }
        return count;
    }
}
发表于 2022-08-09 15:27:24 回复(1)
import java.util.*;
public class Solution {
    public int NumberOf1(int n) {
        int count=0;
        while(n!=0){
            n=n&(n-1);
            count++;
        }
        return count;
    }
}

发表于 2022-08-02 18:43:22 回复(0)
public class Solution {
    public int NumberOf1(int n) {
		return Integer.bitCount(n);
    }
}

发表于 2022-06-06 18:08:41 回复(1)
public class Solution {
    public int NumberOf1(int n) {
         int ans = 0;
        while (n != 0) {
            n = n & (n - 1);
            ans++;
        }
        return ans;
    }
}

发表于 2022-05-24 11:04:47 回复(0)
public class Solution {
    public int lowbit(int x){
        return x&-x;
    }
    public int NumberOf1(int n) {
        int ans=0;
        while(n!=0){
            n=n^lowbit(n);
            ans++;
        }
        return ans;
    }
}l

利用树状数组的lowbit
发表于 2022-04-25 09:21:22 回复(0)
>> 有符号右移,负数补1正数补0
>>>无符号右移,都补0
发表于 2022-03-27 22:44:19 回复(0)
public int NumberOf1(int n) {
    int a = 0;
    while (n != 0) {
        if ((n & 1) == 1) {
            a++;
        }
        n = n>>>1;
    }
    return a;
}

发表于 2022-02-17 10:18:16 回复(0)
普通解:
public class Solution {
    public int NumberOf1(int n) {
        // 定义计数器
        int count=0;
        // int类型是32位,所以循环32次
        for(int i=0;i<32;i++){
            // 每一次都要检查末位是否为1
            if( (n&1)==1 ){
                count++;
            }
            // 右移一位
            n = n>>1;
        }
        return count;
    }
}
优化:
public class Solution {
    public int NumberOf1(int n) {
        // 定义计数器
        int count=0;
        // int类型是32位,所以循环32次
        while(n!=0){
            // 每一次都要检查末位是否为1
            if( (n&1)==1 ){
                count++;
            }
            // 右移一位(使用无符号右移,补码最高位补0而不是1,所以最多循环32次)
            n = n>>>1;
        }
        return count;
    }
}



发表于 2022-01-08 23:03:18 回复(0)
public class Solution {
    public int NumberOf1(int n) {
  
        
        return Integer.bitCount(n);
       
    }
}
发表于 2022-01-03 11:13:51 回复(0)
public class Solution {
    public int NumberOf1(int n) {
        String bB = Integer.toBinaryString(n);
        int sum = 0;
        for(int i=0; i<bB.length(); i++){
            if(bB.charAt(i)=='1'){
                sum++;
            }
        }
        return sum;
    }
    /*
    //思想:用1(1自身左移运算,其实后来就不是1了)和n的每位进行位与,来判断1的个数
        private static int NumberOf1_low(int n) {
        int count = 0;
        int flag = 1;
        while (flag != 0) {
            if ((n & flag) != 0) {
                count++;
            }
            flag = flag << 1;
        }
        return count;
    }
    //--------------------最优解----------------------------
    public static int NumberOf1(int n) {
        int count = 0;
        while (n != 0) {
            ++count;
            // 消除最右边的1
            n = (n - 1) & n;
        }
        return count;
    }
    public static void main(String[] args) {
        //使用n=10,二进制形式为1010,则1的个数为2;
        int n = -10;
        System.out.println(n + "的二进制中1的个数:" + NumberOf1(n));
    }
    */
}
发表于 2021-12-02 21:14:15 回复(0)

无符号右移就好了

public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        while (n!=0){
            if((n&1)==1)count++;
            n >>>= 1;
        }
        return count;
    }
}
发表于 2021-11-14 15:14:11 回复(0)

问题信息

难度:
248条回答 228451浏览

热门推荐

通过挑战的用户

查看代码