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. Linux 终端显示 Git 当前所在分支

    function git_branch { branch="`git branch 2>/dev/null | grep "^\*" | sed -e " ...

  2. python 中main函数总结

    Python使用缩进对齐组织代码的执行,所有没有缩进的代码(非函数定义和类定义),都会在载入时自动执行,这些代码,可以认为是Python的main函数. 每个文件(模块)都可以任意写一些没有缩进的代码 ...

  3. TCPflow:在Linux中分析和调试网络流量的利器(转)

    TCPflow是一款功能强大的.基于命令行的免费开源工具,用于在Unix之类的系统(如Linux)上分析网络流量.它可捕获通过TCP连接接收或传输的数据,并存储在文件中供以后分析,采用的格式便于协议分 ...

  4. Content Security Policy的学习理解

    以下内容转载自 http://www.cnblogs.com/alisecurity/p/5924023.html 跨域脚本攻击 XSS 是最常见.危害最大的网页安全漏洞. 为了防止它们,要采取很多编 ...

  5. [python]MS17-010自动化扫描脚本

    一种是3gstudent分享的调用Nsa泄露的smbtouch-1.1.1.exe实现验证,另一种是参考巡风的poc.这里整合学习了下两种不同的方法. import os import fileinp ...

  6. QDUOJ 分辣条-01背包恰好装满情况

    分辣条 发布时间: 2016年6月26日 20:36   最后更新: 2016年6月26日 20:37   时间限制: 1000ms   内存限制: 128M 描述 “你喝的酸奶是我买的,辣条也是我买 ...

  7. supervisor 启动 celery 及启动中的问题

    一.前言 本教程重点在于supervisor的配置过程,celery的安装配置请参考其他教程 二.安装supervisor 1.安装命令 pip install supervisor # superv ...

  8. XSL简单实例

    利用xsl可以做到将数据和格式完全分离,下面比较一个xml+css和xml+xsl的区别. XML + css resume.xml文件内容如下: <?xml version="1.0 ...

  9. vue的踩坑路

    ------>axios模拟get json一直拿不到文件,先把data放到根目录,再去dev-server.js(就是npm执行的那个文件)里面设置静态资源访问路径app.use('/data ...

  10. E20180506-hm

    更新: 2019/02/19 原来忘记分类,把此博文归入单词类 criterion n. 规范; (批评.判断等的) 标准,准则; criteria  n. (批评.判断等的) 标准,准则( crit ...