leet-code/src/main/java/leetcode/editor/cn/ErJinZhiZhong1deGeShuLcof.java

62 lines
1.6 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//请实现一个函数,输入一个整数(以二进制串形式),输出该数二进制表示中 1 的个数。例如,把 9 表示成二进制是 1001有 2 位是 1。因此如果输入
//9则该函数输出 2。
//
//
//
// 示例 1
//
//
//输入00000000000000000000000000001011
//输出3
//解释:输入的二进制串 00000000000000000000000000001011 共有三位为 '1'。
//
//
// 示例 2
//
//
//输入00000000000000000000000010000000
//输出1
//解释:输入的二进制串 00000000000000000000000010000000 共有一位为 '1'。
//
//
// 示例 3
//
//
//输入11111111111111111111111111111101
//输出31
//解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'。
//
//
//
// 提示:
//
//
// 输入必须是长度为 32 的 二进制串 。
//
//
//
//
// 注意:本题与主站 191 题相同https://leetcode-cn.com/problems/number-of-1-bits/
// Related Topics 位运算
// 👍 139 👎 0
package leetcode.editor.cn;
//剑指 Offer 15:二进制中1的个数
public class ErJinZhiZhong1deGeShuLcof {
public static void main(String[] args) {
//测试代码
Solution solution = new ErJinZhiZhong1deGeShuLcof().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
return Integer.bitCount(n);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}