HTMLify

LeetCode - Counting Bits - Go
Views: 3 | Author: abh
func one_count(n int) int {
    var count int
    for ;n!=0; {
        if n%2==1 {
            count++
        }
        n /= 2
    }
    return count
}
func countBits(n int) []int {
    var ans []int
    for i:=0;i<=n;i++ {
        ans = append(ans, one_count(i))
    }
    return ans
}

Comments