题目就是:

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.

这就是考十进制转二进制的题,学到了#include <cstdin>这个头文件,还有简单的转换算法,算法里边数据类型,别随手定义成了int!!!

  1. class Solution {
  2. public:
  3. int hammingWeight(uint32_t n)
  4. {
  5. uint32_t iRet = ;
  6. uint32_t flag = ;
  7. uint32_t iRes = n;
  8. uint32_t iLeft = ;
  9. while(flag != )
  10. {
  11. flag = iRes / ;
  12. iLeft = iRes - flag * ;
  13. if(iLeft == )
  14. {
  15. ++iRet;
  16. }
  17. iRes = flag;
  18. }
  19. return iRet;
  20. }
  21. };

写的不好,但多code多学习总是有用。

LeetCode 191:number of one bits的更多相关文章

  1. LeetCode OJ:Number of 1 Bits(比特1的位数)

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

  2. leetcode:Number of 1 Bits

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

  3. LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告

    题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

  4. [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  5. LeetCode算法题-Number of 1 Bits(Java实现)

    这是悦乐书的第186次更新,第188篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第45题(顺位题号是191).编写一个带无符号整数的函数,并返回它所具有的"1 ...

  6. LeetCode(476): Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  7. LeetCode OJ:Number of Islands(孤岛计数)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. LeetCode 693 Binary Number with Alternating Bits 解题报告

    题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...

  9. Leetcode 762. Prime Number of Set Bits in Binary Representation

    思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...

随机推荐

  1. MySQL☞where与like

    1.无条件查询语句(查询表中所有数据) select * from 表名 2.查询某些列的数据(指定列) select 列名1,列名2,列名3 from 表名 3.条件查询语句 select 列名1, ...

  2. Python不同进制之间的转换

    不同的进制 二进制    0b101 以数字0和字母b打头的表示二进制数 如果出现大于等于2的数 会抛出SyntaxError异常 八进制    0711 以数字0打头的数字表示八进制数 如果出现大于 ...

  3. Android Studio的初体验

    在机缘巧合之下遇到了安卓开发,接触了Android Studio开始了漫长的改bug的道路,以下为简易版心酸历程 首先我需要成功安装Android Studio,由于我过于叛逆以及为了避免出错于是从一 ...

  4. 在网站中配置MIME类型

    经常会遇到这样的情况,某种类型的文件不能够正常下载,*.7z,自定义的文件类型等,需要在配置文件里配置后才能正常下载. 打开Web.Config文件: <system.webServer> ...

  5. Java集合(2)——深入理解ArrayList、Vector和LinkedList

    回顾 Java集合主要分为两个体系结构,Collection和Map.这篇博客主要介绍Collection子接口List下的三个经常使用的实现类:ArrayList.Vector和LinkedList ...

  6. maven中如何得到父工程的位置

    目前的项目是一个父子工程项目,想要在子工程的pom文件中操作父工程目录下的资源. maven官方提供了标准的写法,比如parent.basedir之类的,网上可以找到很多,但尝试了之后就是不识别. 搞 ...

  7. JAVA实现定时器功能

    在接口开发时,有一种开发模式叫定时器模式,可以理解为每经过一段预设的时间就会执行一次事件,而在我们的工作中,这个事件所实现的功能一般是将两个系统的数据信息进行同步,这样就实现了两个系统通过接口进行对接 ...

  8. 【bzoj1180】[CROATIAN2009]OTOCI LCT

    题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes”,并且在结点 ...

  9. Java Integer比较

    今天看微信做了一个选择题,对Integer比较结果有点意外,题目如下: public static void main(String[] args) { Integer a = 1; Integer ...

  10. TCP/IP Note3

    TCP/IP协议 TCP/IP是不同的通信协议的大集合. 协议族:TCP/IP是基于TCP和IP这两个最初的协议之上的不同的通信协议的大集合. 1. TCP - 传输控制协议 TCP用于从应用程序到网 ...