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. Linux(centos6.0)下安装Node.js以及使用
  2. CSipSimple通话记录分组
  3. 扇贝编程python是干嘛的-产品观察 | 以对话式互动学习撬动转化,扇贝编程瞄准职教市场...
  4. 『树上匹配 树形dp』
  5. 通用SQL数据库查询语句精华使用简介
  6. QT的QGLShaderProgram类的使用
  7. 实现option上下移动_js: 实现Select的option上下移动 | 学步园
  8. java+cache使用方法_java相关:springboot使用GuavaCache做简单缓存处理的方法
  9. 最新丁林松老师全程讲解QT高级编程技术(完整)
  10. Redhat下7-Zip的安装和使用
  11. 单片机c语言串转并的IO实验,74LS164 串入并出实验
  12. 写给独自站在人生面前的自己1-java加密算法
  13. 记录一个问题:jdbc连接数据库很慢【能连上,但很慢】、同理,任何应用连接慢也可以尝试用该方法
  14. 寒江独钓——Windows内核安全编程
  15. VMware ESxi 7.0定时关机
  16. 仓库库存表的三种设计方式
  17. Spring AOP 学习笔记
  18. 网站刷关键词_如何提升网站关键词及长尾词的排名 - 百度排名提升软件
  19. 研究生考试复习有哪些基本的思路或准备?
  20. springboot集成netty实现代理服务器

热门文章

  1. JAVA通过JDBC连接并操作MySQL数据库
  2. *Boosting*笔记
  3. HttpWebRequest在GetResponse时总是超时
  4. Linux(Centos)之安装Redis及注意事项
  5. JavaWeb基础—数据库连接池DBCP、C3P0
  6. iOS-iOS9.Plist插入网络安全xml
  7. ionic+angularJS+iOS混合开发app的学习资料介绍和基本步骤(干货)
  8. VS2012发布网站详细步骤
  9. 为了测试Writer的发图功能,也为了让girls现身。
  10. R语言警告:Cannot compute exact p-value with ties的处理方法