Question

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.

Code

#include<iostream>
using namespace std;class Solution {
public:int hammingWeight(uint32_t n) {int count = 0;while (n != 0) {count++;n = (n - 1)&n;}return count;}
};int main() {Solution so;uint32_t n=11;cout<<so.hammingWeight(n)<<endl;system("pause");return 0;
}

【Leetcode】Number of 1 Bits(easy)相关推荐

  1. 【Leetcode】Number of Islands

    题目链接:https://leetcode.com/problems/number-of-islands/ 题目: Given a 2d grid map of '1's (land) and '0' ...

  2. 【leetcode】length of last word (easy)

    题目: 输入字符串 s,返回其最后一个单词的长度 如 s="Hello World"   返回5 s="Hello World    "   返回5 s=&qu ...

  3. 【Leetcode】Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 思路:若使用[Leetcode]Reverse I ...

  4. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

  5. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【LeetCode】《剑指Offer》第Ⅴ篇⊰⊰⊰ 39 - 47题

    [LeetCode]<剑指Offer>第Ⅴ篇⊰⊰⊰ 39 - 47题 文章目录 [LeetCode]<剑指Offer>第Ⅴ篇⊰⊰⊰ 39 - 47题 39. 数组中出现次数超过 ...

  8. 【LeetCode】《剑指Offer》第Ⅰ篇⊰⊰⊰ 3 - 11题

    [LeetCode]<剑指Offer>第Ⅰ篇⊰⊰⊰ 3 - 11题 文章目录 [LeetCode]<剑指Offer>第Ⅰ篇⊰⊰⊰ 3 - 11题 03. 数组中重复的数字(ea ...

  9. 【LeetCode】第374题——猜数字大小(难度:简单)

    [LeetCode]第374题--猜数字大小(难度:简单) 题目描述 解题思路 代码详解 注意点 题目描述 猜数字游戏的规则如下: 每轮游戏,我都会从 1 到 n 随机选择一个数字. 请你猜选出的是哪 ...

最新文章

  1. 你写的ML代码占多少内存?这件事很重要,但很多人还不懂
  2. 直方图中最大的矩形(遍历与单调栈)
  3. 程序员修神之路--做好分库分表其实很难之二
  4. 微软Azure AspNetCore微服务实战第2期(内附PPT下载)
  5. 云服务器主机内网 ip 和外网 ip 的区别
  6. 机器人当中的摩擦力(未完)
  7. (转)CentOS7安装Nginx1.14.2
  8. harbor1.4.0高可用部署
  9. cmd进入mysql界面的命令
  10. 微软所有正版软件下载网站ITELLYOU
  11. 关于不同操作系统下浏览器兼容问题
  12. (JS)统计重复个数
  13. matlab 双馈,基于Matlab的双馈调速系统研究
  14. 三次握手和四次挥手详解
  15. 一般英文(java)
  16. jquery操作样式
  17. 互联网大咖看了都忍不住点赞的电脑配置指南【电脑小白必备】【十年私藏公开分享】
  18. SQLServer日期函数及查询当日昨日本周本月本季度本年数据
  19. 短视频剪辑制作教学:编辑短视频时需要注意的三个方面
  20. 鱼c笔记——Python字典(二)

热门文章

  1. 办公室局域网主机为静态ip时配置路由器由器的方案
  2. PHP+SQLite3简约网址导航、书签管理器网站源码/百素导航系统
  3. LockSupport 中的 park 与 unpark
  4. 微软687亿美金收购动视暴雪,元宇宙的七龙珠微软还差几颗?
  5. How to install Windows device driver, Vista, Vista x64, WinXP, WinXP x64 Window
  6. 大学生就业培训公司《一起求职》精心整理的各行业的秋招简历模板,含金量高非常实用,希望对大家有帮助!
  7. 支付宝生活号接入方案
  8. (微信小程序毕业设计)基于微信小程序奶茶在线下单系统
  9. 推荐5款设计软件,总能找到你爱的
  10. python 某个数是不是在某个范围内_这个Python入门教程,国内12岁小孩都能做,是小白的入门不二之选...