191. Number of 1 Bits
191. Number of 1 Bits
题目
Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
Note:
- Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
- In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 , the input represents the signed integer.
-3
.
Example 1:
Input: n = 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
Example 2:
Input: n = 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
Example 3:
Input: n = 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
Constraints:
- The input must be a binary string of length
32
.
Follow up: If this function is called many times, how would you optimize it?
题目大意
编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(也被称为 汉明重量)。
解题思路
思路一:循环
可以直接循环检查给定整数 n
的二进制位的每一位是否为 1
。
当检查第 i
位时,可以让 n
与 2^i
进行 与 运算,当且仅当 n
的第 i
位为 1
时,运算结果不为 0
。
时间复杂度:O(k)
,其中 k=32
,一共需要检查 32
位。
空间复杂度:O(1)
,只需要常数的空间保存若干变量。
思路二:位运算
由于 n & (n−1)
会 n
的二进制位中的最低位的 1
变为 0
,如:6 & (6−1) = 4
, 6 = (110)_2
, 4 = (100)_2
,运算结果 4
即为把 6
的二进制位中的最低位的 1
变为 0
之后的结果。
可以利用这个位运算的性质,不断让当前的 n
与 n - 1
做与运算,直到 n
变为 0
即可。因为每次运算会使得 n
的最低位的 1
被翻转,因此运算次数就等于 n
的二进制位中 1
的个数。
代码
/**
* @param {number} n - a positive integer
* @return {number}
*/
var hammingWeight = function (n) {
let res = 0;
for (let i = 0; i < 32; i++) {
if (n & (1 << i)) {
res++;
}
}
return res;
};
/**
* @param {number} n - a positive integer
* @return {number}
*/
var hammingWeight = function (n) {
let res = 0;
while (n) {
n &= n - 1;
res++;
}
return res;
};
相关题目
- [190. 颠倒二进制位](https://leetcode.com/problems/reverse-bits)
- [231. 2 的幂](https://leetcode.com/problems/power-of-two)
- [338. 比特位计数](https://leetcode.com/problems/counting-bits)
- [401. 二进制手表](https://leetcode.com/problems/binary-watch)
- [461. 汉明距离](https://leetcode.com/problems/hamming-distance)
- [693. 交替位二进制数](https://leetcode.com/problems/binary-number-with-alternating-bits)
- [762. 二进制表示中质数个计算置位](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation)