一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

For example, the 32-bit integer ’11’ has binary representation 00000000000000000000000000001011, so the function should return 3.

(二)解题

题目大意:计算一个数的二进制表达式中1的个数

解题思路:剑指offer上的老题了。

把一个整数减去1,然后与原整数做与运算,会把该整数最右边的一个1变成0,要计算1的个数,就循环上述操作,直到这个数为0为止。

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

【一天一道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 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 ...

  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

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

  6. 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 ...

  7. (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 ...

  8. 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 ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. Python【第二课】 字符串,列表,字典,集合,文件操作

    本篇内容 字符串操作 列表,元组操作 字典操作 集合操作 文件操作 其他 1.字符串操作 1.1 字符串定义 特性:不可修改 字符串是 Python 中最常用的数据类型.我们可以使用引号('或&quo ...

  2. 第三节基础篇—SQL的约束

    1.约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性.唯一性.本节实验将在实践操作中熟悉 MySQL 中的几种约束. 约束分类: 2.删除数据库语句为DROP DATABASE ...

  3. Lintcode393 Best Time to Buy and Sell Stock IV solution 题解

    [题目描述] Say you have an array for which the i th element is the price of a given stock on day i. Desi ...

  4. 原生JS模拟百度搜索关键字与跳转

    <style type="text/css"> *{ margin: 0; padding: 0; } #text{ width: 300px; height: 30p ...

  5. Qt与FFmpeg联合开发指南(二)——解码(2):封装和界面设计

    与解码相关的主要代码在上一篇博客中已经做了介绍,本篇我们会先讨论一下如何控制解码速度再提供一个我个人的封装思路.最后回归到界面设计环节重点看一下如何保证播放器界面在缩放和拖动的过程中保证视频画面的宽高 ...

  6. CSS布局套路

    这篇笔记的目的是记录分别应用float和flex布局的方法.主要是对遇到的问题进行总结. 1.float布局 总结: 1.1 使用float布局要清除浮动,清除的方法是,在父元素添加如下样式 .cle ...

  7. Node.js URL

    稳定性: 3 - 稳定 这个模块包含分析和解析 URL 的工具.调用 require('url') 来访问模块. 解析 URL 对象有以下内容,依赖于他们是否在 URL 字符串里存在.任何不在 URL ...

  8. Node.js 系统

    稳定性: 4 - API 冻结 提供一些基本的操作系统相关函数. 使用 require('os') 访问这个模块. os.tmpdir() 返回操作系统的默认临时文件夹 os.endianness() ...

  9. Go 语言结构体

    Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型. 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合. 结构体表示一项记录,比如保存图书馆的书籍记录,每 ...

  10. Gradle 1.12用户指南翻译——第46章. Java 库发布插件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...