diff --git a/go/338-Counting-Bits.go b/go/338-Counting-Bits.go new file mode 100644 index 000000000..19d014359 --- /dev/null +++ b/go/338-Counting-Bits.go @@ -0,0 +1,12 @@ +func countBits(n int) []int { + dp := make([]int, n + 1) + offset := 1 + + for i := 1; i <= n; i++ { + if offset * 2 == i { + offset = i + } + dp[i] = 1 + dp[i - offset] + } + return dp +} \ No newline at end of file