1. Number of 1 Bits

Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).

Example 1:

Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.

Example 2:

Input: 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.

Example 3:

Input: 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.

Note:

  • Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 above the input represents the signed integer -3.

解1 移位+统计1的个数。每次看n的最后一位是不是1,然后右移一位,直到n==0成立

class Solution {
public:
int hammingWeight(uint32_t n) {
int res = 0;
while(n){
if(n & 1 == 1)res++;
n >>= 1;
}
return res;
}
};

解2 将n中的1全部翻转。n&(n-1)会使n翻转最后一个1

class Solution {
public:
int hammingWeight(uint32_t n) {
int res = 0;
while(n){
res++;
n &= n - 1;
}
return res;
}
};

【刷题-LeetCode】191 Number of 1 Bits的更多相关文章

  1. Leetcode#191. Number of 1 Bits(位1的个数)

    题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...

  2. LN : leetcode 191 Number of 1 Bits

    lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...

  3. 【leetcode刷题笔记】Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  4. LeetCode 191. Number of 1 bits (位1的数量)

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  5. [LeetCode] 191. Number of 1 Bits 二进制数1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  6. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...

  7. Java for LeetCode 191 Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  8. (easy)LeetCode 191.Number of 1 Bits

    Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...

  9. Java [Leetcode 191]Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...

  10. [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)

    描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...

随机推荐

  1. 遇到奇葩的现象,给input的id为10的value属性赋值为6,但是怎么显示的时候值还是原先的9的

    遇到奇葩的现象,给input的id为10的value属性赋值为6,但是怎么显示的时候值还是原先的9的 后来发现原来是id标签重复了,所有以后得注意了. 人员信息input的id属性与隐藏input属性 ...

  2. ajax 有终止请求 abort 那 axios 有没有,怎么实现

    见代码 class View extends Component { constructor(props){ super(props); this.state = { cancel:null, can ...

  3. JAVA根据A星算法规划起点到终点二维坐标的最短路径

    工具类 AStarUtil.java import java.util.*; import java.util.stream.Collectors; /** * A星算法工具类 */ public c ...

  4. iframe父子页面js之间的调用

    父级页面:mian.html 子页面1:top.html 子页面2:index.html 页面关系 <div onclick="_top()">调用contentTop ...

  5. springboot发送邮件(含附件)

    引入maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  6. FastAPI 学习之路(六十)打造系统的日志输出

    我们要搭建日志系统,我们使用loguru,挺不错的一个开源的日志系统.可以使用 pip install loguru 我们在common创建log.py使用方式也很简单 import os impor ...

  7. Abelian Period

    Abelian Period Accepts: 288 Submissions: 984 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 26 ...

  8. Causal Inference

    目录 Standardization 非参数情况 Censoring 参数模型 Time-varying 静态 IP weighting 无参数 Censoring 参数模型 censoring 条件 ...

  9. 5分钟搭建wordpress个人博客网站——宝塔傻瓜式部署,无坑系列,附赠主题和md插件[2021-12-31]

    一.前言 自从买了服务器,小编已经马不停蹄的学了两天服务搭建的知识,问了很多大佬,快速搭建自己的博客网站.有四种方式,我在这里全部分享给大家.自己已经搭建好,欢迎大家过来看一下,给你提供个思路哈! 小 ...

  10. rapidjson解析与构造实例

    void rapidjson1(){ rapidjson::StringBuffer s; rapidjson::Writer<rapidjson::StringBuffer> write ...