Skip to content

Commit

Permalink
Create: 338-Counting-Bits.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Sep 14, 2022
1 parent 9e82845 commit b26f565
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions go/338-Counting-Bits.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b26f565

Please sign in to comment.