题目

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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

代码

1.递归运算,虽然leet不通过,因为时间原因
public class Solution {// you need to treat n as an unsigned valuepublic static int hammingWeight(int n) {if(n==0) return 0;if(n==1) return 1;int total=1;int index=1;while(n-total*2>=0){total=total*2;}n=n-total;index=index+hammingWeight(n);return index;}

2.用位运算方法

public class Solution {// you need to treat n as an unsigned valuepublic int hammingWeight(int n) {if(n==0) return 0;int i=0;while(n!=0){n=n & (n-1);i++;}return i;}
}
代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)相关推荐

  1. LeetCode 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  2. [勇者闯LeetCode] 191. Number of 1 Bits

    [勇者闯LeetCode] 191. Number of 1 Bits Description Write a function that takes an unsigned integer and ...

  3. LeetCode - Easy - 191. Number of 1 Bits

    Topic Bit Manipulation Description https://leetcode.com/problems/number-of-1-bits/ Write a function ...

  4. leetcode python3 简单题191. Number of 1 Bits

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百九十一题 (1)题目 英文: Write a function that ta ...

  5. 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 ...

  6. 【LeetCode】191. Number of 1 Bits

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

  7. Leet Code OJ 191. Number of 1 Bits [Difficulty: Easy]

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

  8. Leetcode PHP题解--D57 762. Prime Number of Set Bits in Binary Representation

    2019独角兽企业重金招聘Python工程师标准>>> D57 762. Prime Number of Set Bits in Binary Representation 题目链接 ...

  9. Leetcode PHP题解--D58 693. Binary Number with Alternating Bits

    2019独角兽企业重金招聘Python工程师标准>>> D58 693. Binary Number with Alternating Bits 题目链接 693. Binary N ...

最新文章

  1. 基于libmad库的MP3解码简析
  2. springcloud项目打包_SpringCloud 快速入门
  3. Resilio(BtSync)搭建
  4. 用java写游戏_用java写的扫雷游戏
  5. rds_dbsync数据源同步工具
  6. 前端---JavaScript基础4
  7. CF938G Shortest Path Queries(线性基,线段树分治,并查集)
  8. [转载] Python数学实验与建模 课后习题第1章解析
  9. 【bzoj3573】 Hnoi2014—米特运输
  10. synchronized锁和lock锁 队列中线程执行顺序对比
  11. 中文圣经 for Android
  12. 中景园0.96寸 OLED 显示屏 学习笔记
  13. 32款 jQuery UI框架开源软件
  14. DM数据库体系结构介绍
  15. mysql里一个中文多少个字节_mysql里中文占多少个字节?
  16. Java~大厂面试八股文~强烈推荐视频
  17. 百度杯”CTF比赛(十一月场)
  18. Windows禁用和启用触屏功能
  19. ReSpeaker 4-Mic 声源定位
  20. linux 进程学习笔记-进程信号sigal

热门文章

  1. 卡写入速度_看清商家买相机送SD卡的套路,一文教你掌握存储卡选购秘诀
  2. 立创eda封装转cadence_立创EDA05、检查封装、生成PCB
  3. js 到 jsp 传输数据
  4. qt4.8添加mysql驱动_Windows下编译Qt4.8 MySQL数据库驱动
  5. matlab中if语句的用法_if语句的基本用法
  6. c 定义结构体时提示应输入声明_C语言结构体的坑很多,这6大方法千万要记住!...
  7. 热点的ap频段哪个快_小米9手机热点无法使用:建议2.4G+5G 信号同时输出,提高兼容性...
  8. 苹果cms的php.ini,苹果cms安装及配置详细教程
  9. 用邮箱实现多事件的单向同步
  10. 任务间共享资源问题总结