每次有空做每日一题,都碰到简单题。。。。。。
class Solution:
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
ans = 0
for i in range(len(nums)):
cnt = 0
t = i
while t > 0:
cnt += 1 if t & 1 == 1 else 0
t >>= 1
ans += nums[i] if cnt == k else 0
return ans