Brute force solution:
Iterate 32 times, each time determining if the ith bit is a ’1′ or not. This is probably the easiest solution, and the interviewer would probably not be too happy about it. This solution is also machine dependent (You need to be sure that an unsigned integer is 32-bit). In addition, this solution is not very efficient too, as you need to iterate 32 times no matter what.

    static int countSetBits(long n){int count = 0;while(n!=0){count += n & 1;n >>= 1;}return count;}

Best solution:
Hint: Remember my last post about making use x & (x-1) to determine if an integer is a power of two? Well, there are even better uses for that! Remember every time you perform the operation x & (x-1), a single 1 bit is erased?

The following solution is machine independent, and is quite efficient. It runs in the order of the number of 1s. In the worst case, it needs to iterate 32 times (for a 32-bit integer), but a case such as the number ’8′ would only need to iterate 1 time.

int number_of_ones(unsigned int x) {int total_ones = 0;while (x != 0) {x = x & (x-1);total_ones++;}return total_ones;
}

转载于:https://www.cnblogs.com/leetcode/p/4020088.html

Number of 1 bits相关推荐

  1. LeetCode 191 Number of 1 Bits

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

  2. 领扣-191 位1的个数 Number of 1 Bits MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

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

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

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

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

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

  6. leetcode Number of 1 Bits

    题目连接 https://leetcode.com/problems/number-of-1-bits/ Number of 1 Bits Description Write a function t ...

  7. 【LeetCode】191. Number of 1 Bits

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

  8. [LeetCode] Number of 1 Bits Reverse Integer - 整数问题系列

    目录: 1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历] 3.Reverse Integer - 翻 ...

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

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

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

最新文章

  1. C语言中compile time assert的实现
  2. 【知了堂学习笔记】MySQL数据库常用的SQL语句整理
  3. xml--Schema约束
  4. 用VC写Assembly代码(5) --循环的使用(一)
  5. GDB Checkpoints
  6. PaddleDetection的学习笔记
  7. 使用 Cobbler 自动化和管理系统安装
  8. 西门子s7-200解密软件下载_高邮哪里有西门子三菱PLC编程学习班?多久能学会?...
  9. 苹果绕id工具_绕ID教程(iOS13.313.3.1)
  10. 机器视觉入门,网友推荐的书目
  11. distpicker.js 三级联动,修改地址时设置默认值
  12. 【微信授权登录】Scope 参数错误或没有 Scope 权限
  13. win8 修改背景色为绿豆沙颜色
  14. Win10怎么设置不进入屏保也不关闭显示器
  15. vue-cli首页加载速度慢优化vender+vue预加载
  16. python提取一段字符串中的ip地址
  17. ​汽车域控制器架构和OTA的心脏:网关的四大豪门(下)
  18. 过滤器实现用户访问记录日志记录
  19. 领悟《信号与系统》之 信号与系统概论
  20. 你手机里的“微信”,可能是假的!

热门文章

  1. Spark2.0研究
  2. The J2EE Architect's Handbook讀書筆記(二)
  3. 用 gdb 调试 GCC 程序
  4. Python中 sys.argv[]的用法简明解释
  5. Unity热更新技术整理
  6. Latex编译过程中遇到的奇奇怪怪的问题及解决方案
  7. 1048 采药 1049 装箱问题
  8. 能够附加图片的标签控件iOS项目源码
  9. Windows Phone 应用程序生命周期
  10. CSS样式:background-position word-wrap是控制换行的。