Skip to content

Instantly share code, notes, and snippets.

@svaza
Created January 23, 2022 20:52
Show Gist options
  • Save svaza/619ed32e5af765292c08af5dcb8f7354 to your computer and use it in GitHub Desktop.
Save svaza/619ed32e5af765292c08af5dcb8f7354 to your computer and use it in GitHub Desktop.
Counting Bits
public class Solution {
public int[] CountBits(int n) {
if(n == 0) return new int[]{ 0 };
int[] output = new int[n + 1];
output[0] = 0;
output[1] = 1;
for(int i = 2; i < output.Length; i++)
{
output[i] = output[i%2] + output[i/2];
}
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment