본문 바로가기

IT/코딩테스트

[Leetcode] 2859. Sum of Values at Indices With K Set Bits

728x90

https://leetcode.com/problems/sum-of-values-at-indices-with-k-set-bits/

class Solution:
    def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
        sum = 0
        
        for i in range(len(nums)):
            if bin(i)[2:].count('1') == k:
                sum = sum + nums[i]
                
        return sum

 

 

728x90