【LeetCode从零单刷】Number of 1 Bits & Power of Two
2015-04-08 09:53
281 查看
题目:
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.
解答:
其实一开始看题我就懵比了。。。11变00000000000000000000000000001011?后来想清楚11是十进制的“十一”。。。
题意是统计一个数字二进制化之后的,1的个数。最直接的方法就是不断 mod 2 判断是不是 1 之类的。听说这样会超时……想想,二进制,是不是可以引入位运算(一般来说,位运算较于加减乘除之类的计算要快速很多,能用就多用)。
可以将原数不断右移,然后与1位与运算(&),判断最后一位是不是1,是就count++。知道原数变0之后停止。
但是有个问题,假如问题扩展到负数,怎么办?不断右移之后,符号位不变一直是1还不就是无限循环了么?这时候可以引入汉明重量,基于一种事实即 X 与 X-1 位与得到的最低位永远是 0。例如:
减 1 操作将最右边的符号从 0 变到 1,从 1 变到 0。这样操作之后的直接结果是,原数少了个1位。而且对于负数也同样有循环终点。
觉得我讲解的不够清楚的,可以看看:点击打开链接
另外留一个小问题:如何判断一个数是不是2的整数次幂?(提示:看到2,想到位运算!利用汉明重量)
题目:
Given an integer, write a function to determine if it is a power of two.
解答:
这道题原理不难。坑在需要排除负数,因为二的幂(int)类型只会是正数
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= 0) return false;
if(n & (n-1)) return false;
else return true;
}
};
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.
解答:
其实一开始看题我就懵比了。。。11变00000000000000000000000000001011?后来想清楚11是十进制的“十一”。。。
题意是统计一个数字二进制化之后的,1的个数。最直接的方法就是不断 mod 2 判断是不是 1 之类的。听说这样会超时……想想,二进制,是不是可以引入位运算(一般来说,位运算较于加减乘除之类的计算要快速很多,能用就多用)。
可以将原数不断右移,然后与1位与运算(&),判断最后一位是不是1,是就count++。知道原数变0之后停止。
但是有个问题,假如问题扩展到负数,怎么办?不断右移之后,符号位不变一直是1还不就是无限循环了么?这时候可以引入汉明重量,基于一种事实即 X 与 X-1 位与得到的最低位永远是 0。例如:
Expression | Value |
X | 0 1 0 0 0 1 0 0 0 1 0 0 0 0 |
X-1 | 0 1 0 0 0 1 0 0 0 0 1 1 1 1 |
X & (X-1) | 0 1 0 0 0 1 0 0 0 0 0 0 0 0 |
class Solution { public: int hammingWeight(uint32_t n) { int count = 0; while(n) { count++; n = n&(n-1); } return count; } };
觉得我讲解的不够清楚的,可以看看:点击打开链接
另外留一个小问题:如何判断一个数是不是2的整数次幂?(提示:看到2,想到位运算!利用汉明重量)
题目:
Given an integer, write a function to determine if it is a power of two.
解答:
这道题原理不难。坑在需要排除负数,因为二的幂(int)类型只会是正数
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= 0) return false;
if(n & (n-1)) return false;
else return true;
}
};
相关文章推荐
- LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four
- leetcode- Number of 1 bits, power of two, power of three
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
- LeetCode 231,326,342 --Power of Two & Three & Four
- 【小熊刷题】power of two, pow(x, n) <Leetcode 231, 50 Java>
- leetcode解题之231# Power of Two&326. Power of Three Java版 (判断是否为2,或者3 的幂)
- LeetCode Algorithms #231 <Power of Two>
- [LeetCode][数论]Power of Three&Power of Two
- leetcode 231题 power of Two
- Power of Two LeetCode Java
- leetCode(32):Power of Two 分类: leetCode 2015-07-07 10:40 205人阅读 评论(0) 收藏
- leetcode-231 Power of Two
- LeetCode 231: Power of Two
- 【LeetCode 231_整数_位运算】Power of Two
- [leetCode] Power of Two
- leetCode(32):Power of Two
- LeetCode-Power of Two-解题报告
- leetcode:Power of Two
- [LeetCode] Power of Two
- LeetCode Power of Two