Skip to content

Instantly share code, notes, and snippets.

@xynophon
Created April 17, 2016 09:06
Show Gist options
  • Select an option

  • Save xynophon/07f2eea28e21914bdef513e06f3bc687 to your computer and use it in GitHub Desktop.

Select an option

Save xynophon/07f2eea28e21914bdef513e06f3bc687 to your computer and use it in GitHub Desktop.
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