LeetCode 338

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.

Hint:
You should make use of what you have produced already.
Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
Or does the odd/even status of the number help you in calculating the number of 1s?

 

 1 /*************************************************************************
 2     > File Name: LeetCode338.c
 3     > Author: Juntaran    
 4     > Mail: Jacinthmail@gmail.com
 5     > Created Time: 2016年05月10日 星期二 02时33分02秒
 6  ************************************************************************/
 7 
 8 /*************************************************************************
 9     
10     Given a non negative integer number num. 
11     For every numbers i in the range 0 ≤ i ≤ num 
12     calculate the number of 1's in their binary representation 
13     and return them as an array.
14 
15     Example:
16     For num = 5 you should return [0,1,1,2,1,2].
17 
18     Follow up:
19     It is very easy to come up with a solution with run time O(n*sizeof(integer)). 
20     But can you do it in linear time O(n) /possibly in a single pass?
21     Space complexity should be O(n).
22     Can you do it like a boss? 
23     Do it without using any builtin function like __builtin_popcount in c++ 
24     or in any other language.
25     
26     Hint:
27     You should make use of what you have produced already.
28     Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
29     Or does the odd/even status of the number help you in calculating the number of 1s?
30 
31  ************************************************************************/
32 
33 #include "stdio.h"
34 
35 /**
36  * Return an array of size *returnSize.
37  * Note: The returned array must be malloced, assume caller calls free().
38  */
39  
40 /*
41 思路:
42     1:2的次幂都是1
43     2:非2的次幂,比如9,找前一个2的次幂比如8,做差,其二进制中1的个数为差+1
44 */
45 int isPowerOfTwo(int n)
46 {
47     double tmp = log10(n)/log10(2);
48     return tmp == (int)tmp ? 1 : 0;
49 }
50 
51 int * countBits(int num, int* returnSize)
52 {
53     int *result = malloc(sizeof(int)*(num+1));
54     result[0] = 0 ;
55     int last = 0 ;
56     int i;
57     for( i=1 ; i<= num; i++ )
58     {
59         if(isPowerOfTwo(i))
60         {
61             result[i] = 1 ;
62             last = i;
63         }
64         else
65         {
66             result[i] = result[i - last] + 1 ;
67         }
68 //        printf("%d ",result[i]);
69     }
70     *returnSize = num+1 ;
71     return  result;
72 }
73 
74 int main()
75 {
76     int* returnSize;
77     countBits(5,returnSize);
78     
79     return 0;
80 }

 

posted @ 2016-05-10 19:15  Juntaran  阅读(218)  评论(0编辑  收藏  举报