- lintcode: (365) Count 1 in Binary
Count how many 1 in binary representation of a 32-bit integer.
Example
Given 32, return 1
Given 5, return 2
Given 1023, return 9
Challenge
If the integer is n bits with m 1 bits. Can you do it in O(m) time?
題 O1 Check Power of 2 的進階版,x & (x - 1) 的含義爲去掉二進制數中1的最後一位,無論 x 是正數還是負數都成立。
class Solution {
public:
/**
* @param num: an integer
* @return: an integer, the number of ones in num
*/
int countOnes(int num) {
int count=0;
while (num) {
num &= num-1;
count++;
}
return count;
}
};public class Solution {
/**
* @param num: an integer
* @return: an integer, the number of ones in num
*/
public int countOnes(int num) {
int count = 0;
while (num != 0) {
num = num & (num - 1);
count++;
}
return count;
}
}累加計數器即可。
這種算法依賴於數中1的個數,時間複雜度爲
- Number of 1 bits | LeetCode - 評論中有關於不同演算法性能的討論