HTMLify

LeetCode - Sum of Values at Indices With K Set Bits - Go
Views: 2 | Author: abh
func count_set_bit(n int) int {
    var count int
    for ;n>0; {
        if n % 2 == 1 {
            n--
            count++
        }
        n /= 2
    }
    return count
}
func sumIndicesWithKSetBits(nums []int, k int) int {
    var sum int
    for i, n := range nums {
        if k == count_set_bit(i) {
            sum += n
        }
    }
    return sum
}

Comments