問題:
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
Example:
For num = 5 you should return [0,1,1,2,1,2].
Follow up:
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
大意:
給出一個非負整數num。對于 0 ≤ i ≤ num 范圍的每個數計算它們二進制表示的數中的1的個數,并返回它們組成的數組。
例子:
對 num = 5 你應該返回 [0,1,1,2,1,2]。
進階:
- 很容易找到時間復雜度為 O(n*sizeof(integer))的解決方案。但你能不能在線性時間復雜度O(n)中解決呢?
- 康健復雜度需要是O(n)。
你能不能像一個boss一樣做?不要使用像c++中 __builtin_popcount 一樣的內置的函數去做。
思路:
把0~7的二進制表示法的數字列出來,數其中的1的個數,找到一個規律,0對應的數是0,1、2對應的是1個1。往上走只用計算不斷除以2一直除到1后,存在余數為1的次數,加上最后的1,就是該數二進制表示法中1的個數。
注意初始化結果數組的時候容量為 num+1,不是 num。
我的做法時間復雜度應該是O(nlogn)。
初始化int型數組后,數組所有元素默認為0,所以對0的判斷處理可以略去。
代碼(Java):
public class Solution {
public int[] countBits(int num) {
int[] result = new int[num+1];
for (int i = 0; i <= num; i++) {
if (i == 0) result[i] = 0;
else if (i <= 2) result[i] = 1;
else {
int numberOfOne = 1;
int number = i;
while (number > 1) {
numberOfOne += number % 2;
number = number / 2;
}
result[i] = numberOfOne;
}
}
return result;
}
}
他山之石:
public int[] countBits(int num) {
int[] f = new int[num + 1];
for (int i=1; i<=num; i++) f[i] = f[i >> 1] + (i & 1);
return f;
}
這個做法把上面的思想簡化了很多,i&1其實就是看最后一位有沒有1,也就是取余為1。然后加上 f[i >> 1],這個其實就是當前數字除以2后對應的數字的1的個數,所以可以看出我的做法做了很多無用功,因為沒有利用到已經得出的結果,而這個做法的時間復雜度就是O(n)了。
合集:https://github.com/Cloudox/LeetCode-Record