题目就是:

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!!!

class Solution {
public:
int hammingWeight(uint32_t n)
{
uint32_t iRet = ;
uint32_t flag = ;
uint32_t iRes = n;
uint32_t iLeft = ;
while(flag != )
{
flag = iRes / ;
iLeft = iRes - flag * ;
if(iLeft == )
{
++iRet;
}
iRes = flag;
}
return iRet;
}
};

写的不好,但多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. 使用Cydia Substrate Hook Android Java世界

    从来没接触过Android的HOOK,在看雪上找到了一篇HOOK 的文章,但是太复杂了,应该是本地环境问题,测试不成功. 后来搜到Cydia Substrate,看了几篇文章,进入官网查看了一下文档, ...

  2. 系统学习Docker 践行DevOps理念

    Docker代表的容器技术是近两年的大热技术,和人工智能.区块链等热点不同,容器技术的门槛并不高,每一个开发.测试.运维人员都能在日常工作中掌握和使用,是当今IT从业人员的必备技能之一.本课程会带大家 ...

  3. python安装Django

    现在有很多建站系统,很多都是基于php的,比如WordPress. 而Django 是老牌基于Python的CMS框架了,一直听说很强大,甚至曾经很红的Ruby On Rails都参考了它的很多概念, ...

  4. java script 学习

    用JavaScript输出文本 <p>我的第一个段落.</p> <script> document.write(Date()); </script> & ...

  5. 使用IMAGEMAGICK的CONVERT工具批量转换图片格式

    使用IMAGEMAGICK的CONVERT工具批量转换图片格式 http://www.qiansw.com/linux-imagemagick-convert-img.html Home > 文 ...

  6. linux文件系统(ext2)

    一个磁盘可以划分成多个分区,每个分区必须先用格式化工具(例如某种mkfs命令)格式化成某种格式的文件系统,然后才能存储文件,格式化的过程会在磁盘上写一些管理存储布局的信息.下图是一个磁盘分区格式化成e ...

  7. sessionStorage & URL Origin

    sessionStorage & URL Origin same origin https://developer.mozilla.org/en-US/docs/Web/API/Window/ ...

  8. RunKit & NPM

    RunKit + NPM Try any Node.js package right in your browser https://npm.runkit.com/segmentit

  9. [剑指Offer] 38.二叉树的深度

    题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. [思路1]递归 /* struct TreeNode { int v ...

  10. Java IO 之 FileFilter与FilenameFilter

    FileFilter与FilenameFilter可以实现对文件的过滤,他们都是接口,具体的过滤规则需要我们自己编写 1.FileFilter package org.zln.io.file; imp ...