Created
April 17, 2016 09:06
-
-
Save xynophon/07f2eea28e21914bdef513e06f3bc687 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class CountingBits { | |
| public int[] countBits(int num) { | |
| int[] result = new int[num+1]; | |
| int base = 0; | |
| for(int i = 0; i <= num; i++){ | |
| double logvalue = Math.log(i)/Math.log(2); | |
| if(logvalue == (int)logvalue){ | |
| base = i; | |
| result[i] = 1; | |
| }else{ | |
| result[i] = result[i-base] + result[base]; | |
| } | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment