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. 使用 NSData 分类实现,对 NSData 数据类型进行 AES 加密

    一般对NSData的数据类型进行加密,这里就将 .h .m 文件分享出来,有需要的可以直接粘贴使用.     下面是 .h 文件   #import <Foundation/Foundation ...

  2. struts2-045漏洞浅析

    http://blog.csdn.net/rossrocket/article/details/67674290

  3. (Android)截图批处理

    新建一个批处理文件 输入下面的内容 @echo off rem set name=%,%%,%%,%%,%%,%%,% //设置截图文件名称 set screenShotName=%,%%,%%,% ...

  4. [Xcode 实际操作]二、视图与手势-(4)给图像视图添加边框效果

    目录:[Swift]Xcode实际操作 本文将演示给图片添加颜色相框 import UIKit class ViewController: UIViewController { override fu ...

  5. Quantitative proteomic analysis of small and large extracellular vesicles (EVs) reveals enrichment of adhesion proteins in small EVs (文献分享一组-柯酩)

    文献名:Quantitative proteomic analysis of small and large extracellular vesicles (EVs) reveals enrichme ...

  6. LVS-DR VIP和RIP不同网段的配置方法

    http://blog.itpub.net/25723371/viewspace-1446935/

  7. iReport - 无法正常启动的解决方法

    问题与分析 最近需要用到iReport报表工具,但是在启动客户端时却发现只出现了启动界面,很快就界面消失没反应了.反复打开了好几次客户端,都无法正常打开.问了下同事,说是因为jdk升级的原因,以前项目 ...

  8. 下载devc++和codeblocks记录

    dev的安装包自己百度网盘里有 codeblocks官网  下载好后再解压即可,如果不是默认路径安装的话,还会出现检测不到编译器路径问题,解决办法在这.

  9. python 基础(十一) pickle 序列化

    一.pickle序列化的操作 使用说明:可以将数据 转换成2进制 写入到文件中 或者之间返回 做到将数据原样写入 原样取出 import pickle (1) dump 写入文件中 pickle.du ...

  10. django 之 rest framework

    一 二 三 四 五 六 七 八