HTMLify
LeetCode - Sum of Values at Indices With K Set Bits - Go
Views: 298 | Author: abh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 } |