Leetcode NO.191 Number of 1 Bits
来源:互联网 发布:有线键鼠套装推荐 知乎 编辑:程序博客网 时间:2024/05/27 14:13
本题题目要求如下:
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as theHamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011
, so the function should return 3.
代码如下:
class Solution {public: int hammingWeight(uint32_t n) { int count = 0; while (n) { if (n & 1) ++count; n >>= 1; } return count; }};
讨论区里面有更好的算法,时间复杂度是O(m),m为1的个数。。。
0 0
- Leetcode NO.191 Number of 1 Bits
- leetcode:bits:Number of 1 Bits(191)
- [Leetcode 191, Easy] Number of 1 Bits
- leetCode #191 number of 1 bits
- LeetCode 191 Number of 1 Bits
- [LeetCode][191][Number of 1 Bits]
- [leetcode 191]Number of 1 Bits
- LeetCode(191) Number of 1 Bits
- LeetCode #191 Number of 1 Bits
- LeetCode 191: Number of 1 Bits
- LeetCode 191 Number of 1 Bits
- Leetcode #191 Number of 1 Bits
- LeetCode 191 Number of 1 Bits
- LeetCode 191: Number of 1 Bits
- leetcode[191]:Number of 1 Bits
- leetcode-191-Number of 1 Bits
- leetcode 191:Number of 1 Bits
- leetcode: (191) Number of 1 Bits
- windows下PTAM的编译
- POJ1565 水题
- PANIC: Could not open AVD config file:
- 编译PTAM的碎碎念
- SQLSERVER 文件组解决大数据量数据存储
- Leetcode NO.191 Number of 1 Bits
- Matlab lugui
- Matlab pivotgolf
- Matlab interpgui
- Matlab piecelin
- Matlab splinetx
- Matlab pchiptx
- Matlab adaptive quadrature
- Matlab forward Euler