首页 > 其他分享 >[LeetCode] 2859. Sum of Values at Indices With K Set Bits

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

时间:2024-01-25 15:11:59浏览次数:19  
标签:2859 binary Set nums int Sum set representation bits

You are given a 0-indexed integer array nums and an integer k.

Return an integer that denotes the sum of elements in nums whose corresponding indices have exactly k set bits in their binary representation.

The set bits in an integer are the 1's present when it is written in binary.

For example, the binary representation of 21 is 10101, which has 3 set bits.

Example 1:
Input: nums = [5,10,1,5,2], k = 1
Output: 13
Explanation: The binary representation of the indices are:
0 = 0002
1 = 0012
2 = 0102
3 = 0112
4 = 1002
Indices 1, 2, and 4 have k = 1 set bits in their binary representation.
Hence, the answer is nums[1] + nums[2] + nums[4] = 13.

Example 2:
Input: nums = [4,3,2,1], k = 2
Output: 1
Explanation: The binary representation of the indices are:
0 = 002
1 = 012
2 = 102
3 = 112
Only index 3 has k = 2 set bits in its binary representation.
Hence, the answer is nums[3] = 1.

Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 105
0 <= k <= 10

计算 K 置位下标对应元素的和。

给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。
请你用整数形式返回 nums 中的特定元素之 和 ,这些特定元素满足:其对应下标的二进制表示中恰存在 k 个置位。
整数的二进制表示中的 1 就是这个整数的 置位 。
例如,21 的二进制表示为 10101 ,其中有 3 个置位。

思路

对于数组内的每一个数字,我们看一下数字的二进制表达里有几个 1,如果当前数字的二进制表达里 1 的个数等于这个数字在 input 数组内的下标,则把这个数字累加到结果里。

计算数字的二进制表达里有几个 1 等同于 191 题。

复杂度

时间O(n)
空间O(1)

代码

Java实现

class Solution {
    public int sumIndicesWithKSetBits(List<Integer> nums, int k) {
        int res = 0;
        int n = nums.size();
        for (int i = 0; i < n; i++) {
            if (helper(i) == k) {
                res += nums.get(i);
            }
        }
        return res;
    }

    public int helper(int n) {
        int count = 0;
        while (n != 0) {
            n &= n - 1;
            count++;
        }
        return count;
    }
}

相关题目

191. Number of 1 Bits
2859. Sum of Values at Indices With K Set Bits

标签:2859,binary,Set,nums,int,Sum,set,representation,bits
From: https://www.cnblogs.com/cnoodle/p/17987204

相关文章

  • F. Sum of Progression
    原题链接题解关键要素:两次后缀和预处理\(d<\sqrt{n}\)的情况,当\(d\)大于\(\sqrt{n}\)时,直接暴力,总时间复杂度为\(O(n\sqrt{n})\)代码比讲述要清晰,请直接看代码code#include<bits/stdc++.h>#definelllonglongusingnamespacestd;lla[100005]={0};llsufs1[100335][......
  • C转C++速成浅入浅出系列——STL之set
    本系列为应付考研复试用,知识浅入浅出,很多地方不深究细节原理;如有谬误,欢迎大家指出。set【setof:集合】理解为集合。特点是①元素各不相同②元素会自动从小到大排序③初始时无法指定其大小需提供头文件#include<set> 创建注意初始时不能指定其大小。(不能说集合里有5个......
  • 记录--你敢信?比 setTimeout 还快 80 倍的定时器
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助起因很多人都知道,setTimeout是有最小延迟时间的,根据MDN文档setTimeout:实际延时比设定值更久的原因:最小延迟时间中所说:在浏览器中,setTimeout()/setInterval()的每调用一次定时器的最小间隔是4ms,这通常是由于......
  • ChatGPT无法登录报错something went wrong. please make sure your device's date and
    这两天Android在登陆ChatGPT的时候,出现错误:somethingwentwrong.pleasemakesureyourdevice'sdateandtimearesetproperly如下图:这个问题就出现的非常蹊跷,于是我在网上搜索了一圈,很多的教程都指向节点网络问题,但是我的Hostease网络确定没有问题,因此这个问题就快无解了,正......
  • scikit-learn.datasets 机器学习库
    scikit-learn是一个用于Python的机器学习库,提供了大量用于数据挖掘和数据分析的工具。以下是对这些函数和方法的简要描述:clear_data_home:清除数据集目录的内容。dump_svmlight_file:将数据集保存为SVMLight格式的文件。fetch_20newsgroups:下载20个新闻组的文本数据集。f......
  • 通过CanvasRenderer.SetColor和Image.color修改UI组件颜色的区别
    1)通过CanvasRenderer.SetColor和Image.color修改UI组件颜色的区别2)OPPO相关机型没法在Unity启用90或120FPS3)手机输入法中的emoji4)UnityApplicationPatching怎么用这是第369篇UWA技术知识分享的推送,精选了UWA社区的热门话题,涵盖了UWA问答、社区帖子等技术知识点,助力大家更全面......
  • Unity的StreamAssets文件夹
    StreamAssets是一个特殊的文件夹,其中的内容在Unity打包的时候并不会被压缩,完整的带入包体介绍在做一个根据可变配置进行操作的功能时,突然发现在windows中正常的功能在mac上失效了,而且还是部分mac失效。发现StreamAssets在mac某个版本以上就不支持写操作了,搜了一下网上的资料......
  • set用法详解
    ES6中的Set是一种新的数据结构,类似于数组,用于存储有序的数据。Set没有随机访问的能力,不能通过索引来获取具体的某个元素Set中的元素具有唯一性,不允许存储相同的元素。Set本身是一个构造函数,可以用来实例化Set对象。通过add()方法可以向Set中添加元素,如果添加的元......
  • uniapp打包h5在Android的webview中打开出现localStorage.setitem为null的记录
    使用android直接打开h5的链接,报错localStorage.setItem为null原因是要打开Android的webview的存储设置valwebView=findViewById<WebView>(R.id.webview)valsettings=webView.settingssettings.domStorageEnabled=truesettings.datab......
  • Java resultset判断mysql表是否存在
    importjava.sql.*;publicclassCheckTableExistence{publicstaticvoidmain(String[]args)throwsSQLException{Stringurl="jdbc:mysql://localhost:3306/mydatabase";//MySQL服务器地址及数据库名称Stringusername="root"......