C++实现LeetCode(191.位1的个数)

[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 known as the Hamming weight).

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

很简单的一道位操作Bit Manipulation的题,最近新出的三道题都没有啥难度啊,这样会误导新人的,做了这三道得出个LeetCode没啥难度的结论,其实里面好题真的不少,难题也很多,经典题也多,反正就是赞赞赞,32个赞。

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int res = 0;
        for (int i = 0; i < 32; ++i) {
            res += (n & 1);
            n = n >> 1;
        }
        return res;
    }
};

到此这篇关于C++实现LeetCode(191.位1的个数)的文章就介绍到这了,更多相关C++实现位1的个数内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C++实现LeetCode(309.买股票的最佳时间含冷冻期)

    [LeetCode] 309.Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as

  • C++实现LeetCode(186.翻转字符串中的单词之二)

    [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词之二 Given an input string , reverse the string word by word.  Example: Input:  ["t","h","e"," ","s","k","y"," ","i&qu

  • SQL实现LeetCode(183.从未下单订购的顾客)

    [LeetCode] 183.Customers Who Never Order 从未下单订购的顾客 Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything. Table: Customers. +----+-------+ | Id | Name  |

  • SQL实现LeetCode(184.系里最高薪水)

    [LeetCode] 184.Department Highest Salary 系里最高薪水 The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name  | Salary | DepartmentId

  • C++实现LeetCode(190.颠倒二进制位)

    [LeetCode] 190. Reverse Bits 颠倒二进制位 Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Explanation: The input binary string 00000010100101000001111010011100 re

  • C++实现LeetCode(557.翻转字符串中的单词之三)

    [LeetCode] 557.Reverse Words in a String III 翻转字符串中的单词之三 Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode conte

  • C++实现LeetCode(188.买卖股票的最佳时间之四)

    [LeetCode] 188.Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You m

  • SQL实现LeetCode(185.系里前三高薪水)

    [LeetCode] 185.Department Top Three Salaries 系里前三高薪水 The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name  | Salary | DepartmentId | +--

  • C++实现LeetCode(191.位1的个数)

    [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 known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000

  • 聊聊Redis二进制数组Bitmap

    好久没有更新了,之前公司在做 关注/粉丝 这块儿缓存的时候,我选择的就是 Bitmap ,那时是我第一次见识到这种数据数组形式,用到的有 SETBIT , GETBIT , BITCOUNT ,命令如何使用就不说了,今天来仔细看看这三个命令的实现和原理. 选用 bitmap 的考量: 位数组的实现 关注关系需求中 关注对象 和 被关注人 都是 0-几千万 的数据对象,存储这种对应关系时,采用bitmap 这种位数组,明显要比 uid 的 set 方式要节省存储空间,redis 的 内存 是很宝贵

  • Python实现小数转化为百分数的格式化输出方法示例

    本文实例讲述了Python实现小数转化为百分数的格式化输出方法.分享给大家供大家参考,具体如下: 比如将 0.1234 转化为 12.34% 的形式: rate = .1234 print('%.2f%%' % (rate * 100)) 第一个百分号和 .2f 相连,表示浮点数类型保留小数点后两位格式化输出: 然后的两个连续的%%,则最终会输出一个%号出来,有对%进行转义的含义: 将小数(数值)转化为字符串,并赋给其他变量: rate = .1 res = format(res, '.0%')

  • 深入理解C++移位运算符

    关于逻辑移位.算术移位可参见迅雷深大笔试题部分.的一道题. 以前看到C++标准上说,移位运算符(<<.>>)出界时的行为并不确定: The behavior is undefined if the right operand is negative, orgreater than or equal to the length in bits of the promoted left operand. 我当时也没有深究过这个问题.前几天有个网友来信问起这件事,我才发现,这和Intel

  • javascript基础知识

    1.var msg;//声明一个变量,在没有给此变量赋值以前,该变量名为:undefined 2.msg = "hello";//不适用var则可以声明一个全局变量,但是此变量因为是全局的,所以容易引起其他调用的一些问题,故不推荐 3.javaScript有5种基本数据类型,Undefined.Null.Boolean.Number和String:以及一种复杂数据类型:Object 4.var num=076;//代表的八进制,以0开头,假如后面的数值超出8的范围,例:08.079等,

  • linux 随机密码生成工具mkpasswd详解及实例

    linux 随机密码生成工具mkpasswd详解及实例 mkpasswd命令生成随机复杂密码,前提安装expect,然后执行mkpasswd命令即可生成随机的密码. 一.基本的命令安装 安装expect: -l # (密码的长度定义, 默认是 9) -d # (数字个数, 默认是 2) -c # (小写字符, 默认是 3) -C # (大写字符, 默认是 2) -s # (特殊字符, 默认是 1) -v (详细...) -p prog (程序设置密码, 默认是 passwd) 详细参数,用如下命

  • java.text.DecimalFormat用法详解

    简要 DecimalFormat 的 pattern 都包含着 正负子 pattern ,例如 "#,##0.00;(#,##0.00)": /** * Created by Shuai on 2016/7/11. */ public class Main { public static void main(String[] args) { // 正值 BigDecimal bigDecimal = BigDecimal.valueOf(-12211151515151.541666);

  • Java 读写锁实现原理浅析

    最近做的一个小项目中有这样的需求:整个项目有一份config.json保存着项目的一些配置,是存储在本地文件的一个资源,并且应用中存在读写(读>>写)更新问题.既然读写并发操作,那么就涉及到操作互斥,这里自然想到了读写锁,本文对读写锁方面的知识做个梳理. 为什么需要读写锁? 与传统锁不同的是读写锁的规则是可以共享读,但只能一个写,总结起来为:读读不互斥,读写互斥,写写互斥,而一般的独占锁是:读读互斥,读写互斥,写写互斥,而场景中往往读远远大于写,读写锁就是为了这种优化而创建出来的一种机制. 注

  • Redis精确去重计数方法(咆哮位图)

    前言 如果要统计一篇文章的阅读量,可以直接使用 Redis 的 incr 指令来完成.如果要求阅读量必须按用户去重,那就可以使用 set 来记录阅读了这篇文章的所有用户 id,获取 set 集合的长度就是去重阅读量.但是如果爆款文章阅读量太大,set 会浪费太多存储空间.这时候我们就要使用 Redis 提供的 HyperLogLog 数据结构来代替 set,它只会占用最多 12k 的存储空间就可以完成海量的去重统计.但是它牺牲了准确度,它是模糊计数,误差率约为 0.81%. 那么有没有一种不怎么

  • mysql数据类型和字段属性原理与用法详解

    本文实例讲述了mysql数据类型和字段属性.分享给大家供大家参考,具体如下: 本文内容: 数据类型 数值类型 整数型 浮点型 定点型 日期时间类型 字符串类型 补充: 显示宽度与zerofll 记录长度 字段属性 空\不为空值:NULL.NOT NULL 主键:primary key 唯一键:unique key 自增长:auto_increment 默认值:default 字段描述:comment 补充: 复合键 首发日期:2018-04-08 数据类型: mysql的数据类型就是存储数据的类

随机推荐