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.

class Solution {
public:
    int hammingWeight(uint32_t n) {
    int i,res=0;
    for(i=0;i<32;i++)
    {
        if(n%2==1)
            {res++;}
            //return n;
          n = n>>1;//这里要进行赋值,不然不会改变n的值
        //return n;
   
    }
    return res;
    }
};

Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i的更多相关文章

  1. pug.compile() will compile the Pug source code into a JavaScript function that takes a data object (called “locals”) as an argument.

    Getting Started – Pug https://pugjs.org/api/getting-started.html GitHub - Tencent/wepy: 小程序组件化开发框架 h ...

  2. vue引入fastclick设置输入框type="number"报错Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection.的解决办法

    将输入框type设为text,通过正则验证输入的值

  3. [LeetCode] 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】Number of 1 Bits

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

  5. Number of 1 Bits(Difficulty: Easy)

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

  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. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

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

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

随机推荐

  1. This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x0501 or higher is recommended

    VS2005转换成VS2010时出现的问题: This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x05 ...

  2. CSS:CSS 网络安全字体组合

    ylbtech-CSS:CSS 网络安全字体组合 1.返回顶部 1. 常用的字体组合 font-family 属性应该使用若干种字体名称作为回退系统,以确保浏览器/操作系统之间的最大兼容性.如果浏览器 ...

  3. Linux下使用《du》命令查看某文件及目录的大小

    du -ah --max-depth=1     这个是我想要的结果  a表示显示目录下所有的文件和文件夹(不含子目录),h表示以人类能看懂的方式,max-depth表示目录的深度. du -sh 目 ...

  4. Indexed DB入门导学(1) – 51CTO.COM

    在html 5中,其中一个引人注意的新特性,那就是允许使用Indexed DB.… 查阅全文 ›

  5. [转]windows10 1703 鼠标右键打开命令提示符cmd

    https://answers.microsoft.com/zh-hans/windows/forum/windows_10-performance/windows10-1703/8bdfdfea-4 ...

  6. ASP.NET Core会议管理平台实战_汇总贴

    ASP.NET Core会议管理平台实战 课程地址:https://ke.qq.com/course/389673?from=800004097#term_id=100464670 ASP.NET C ...

  7. ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 26. 缓存

    In-Memory 使用IMemeryCache接口 注册缓存 HomeController注入进来 建一个类,用来存缓存的常量 判断缓存里面是否有数据,如果没有就读数据库存起来. 设置缓存事件,可调 ...

  8. UVa 10891 Game of Sum (DP)

    题意:给定一个长度为n的整数序列,两个人轮流从左端或者右端拿数,A先取,问最后A的得分-B的得分的结果. 析:dp[i][j] 表示序列 i~j 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...

  9. E20190214-mt

    roughly  adv. 粗略地; 大致上; 大体上; 粗暴地; equivalent adj. 相等的,相当的,等效的; 等价的,等积的; [化学] 当量的; consult  vi. 咨询; 商 ...

  10. html标签英文全称

    更新: 2017/06/09 最近网页开发,一直会查html和css 感觉之前写的这个太杂了,现在主要是先查这个博文,然后查文档.   -------------------------------- ...