位1的个数

题目链接

位1的个数

题目描述


注意点

  • 输入必须是长度为 32 的 二进制串

解答思路

  • 位运算判断每一位是否为1

代码

public class Solution {
   
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
   
        int res = 0;
        for (int i = 0; i < 32; i++) {
   
            res += (n >> i) & 1;
        }
        return res;
    }
}

关键点

相关推荐

  1. 191. 1个数

    2023-12-14 00:04:04       38 阅读
  2. 191. 1个数

    2023-12-14 00:04:04       32 阅读
  3. 191. 1个数

    2023-12-14 00:04:04       13 阅读
  4. 【力扣】191. 1 个数、485.最大连续 1 个数

    2023-12-14 00:04:04       17 阅读
  5. 力扣:191. 1个数(Python3)

    2023-12-14 00:04:04       42 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-14 00:04:04       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-14 00:04:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-14 00:04:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-14 00:04:04       18 阅读

热门阅读

  1. 2023.12.13 libstdc++ undefined reference to GLIBCXX

    2023-12-14 00:04:04       45 阅读
  2. 52.0/框架(详细版)

    2023-12-14 00:04:04       36 阅读
  3. Go 语言指针

    2023-12-14 00:04:04       45 阅读
  4. PHP中什么是Composer?

    2023-12-14 00:04:04       32 阅读
  5. oracle 查看统计信息

    2023-12-14 00:04:04       32 阅读
  6. vue2 vue-router引入使用详解

    2023-12-14 00:04:04       37 阅读
  7. k8s之高级调度

    2023-12-14 00:04:04       46 阅读