LintCode 1046 · Prime Number of Set Bits in Binary Representation (求比特数和质数题)

这篇具有很好参考价值的文章主要介绍了LintCode 1046 · Prime Number of Set Bits in Binary Representation (求比特数和质数题)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1046 · Prime Number of Set Bits in Binary Representation
Algorithms
Description
Solution16
Notes
Discuss26
Leaderboard
Record

Description
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation.

(Recall that the number of set bits an integer has is the number of 1s present when written in binary. For example, 21 written in binary is 10101 which has 3 set bits. Also, 1 is not a prime.)

1.L, R will be integers L <= R in the range [1, 10^6].
2.R - L will be at most 10000.

Example
Example 1:

Input: L = 6, R = 10
Output: 4
Explanation:
6 -> 110 (2 set bits, 2 is prime)
7 -> 111 (3 set bits, 3 is prime)
9 -> 1001 (2 set bits , 2 is prime)
10->1010 (2 set bits , 2 is prime)
Example 2:

Input: L = 10, R = 15
Output: 5
Explanation:
10 -> 1010 (2 set bits, 2 is prime)
11 -> 1011 (3 set bits, 3 is prime)
12 -> 1100 (2 set bits, 2 is prime)
13 -> 1101 (3 set bits, 3 is prime)
14 -> 1110 (3 set bits, 3 is prime)
15 -> 1111 (4 set bits, 4 is not prime)

解法1:
注意 R in the range [1, 10^6],所以最大数也只有20个比特。
每个数的1数目和,应该就是数组primes中的一个。primes = {2, 3, 5, 7, 11, 13, 17, 19};文章来源地址https://www.toymoban.com/news/detail-738239.html

class Solution {
public:
    /**
     * @param l: an integer
     * @param r: an integer
     * @return: the count of numbers in the range [L, R] having a prime number of set bits in their binary representation
     */
    int countPrimeSetBits(int l, int r) {
        int primeBits = 0;
        vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19};
        set<int> primeSet(primes.begin(), primes.end());
        int res = 0;
        for (int i = l; i <= r; i++) {
            int numBits = calcBits(i);
            if (primeSet.find(numBits) != primeSet.end()) {
                res++;
            }
        }
        return res;
    }
private:
    int calcBits(int x) {
       int numBits = 0;
       while (x) {
           x = x & (x - 1);
           numBits++;
       }
       return numBits;
    }
};

到了这里,关于LintCode 1046 · Prime Number of Set Bits in Binary Representation (求比特数和质数题)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Java异常 #Number of lines annotated by Git is not equal to number of lines in the file, check file …

    在项目中某个 java 文件左边栏右键查看代码版本履历(Annotate)时无法显示,IDEA 提示:Number of lines annotated by Git is not equal to number of lines in the file, check file encoding and line separators.   这个问题涉及到不同操作系统下文本文件的换行符差异引起的。在不同操作系统中,文本文件的

    2024年02月03日
    浏览(35)
  • LeetCode 2475. Number of Unequal Triplets in Array【数组,排序,哈希表】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(36)
  • ImportError: ERROR: recursion is detected during loading of “cv2“ binary extensions. Check OpenCV in

    1. import cv2错误 ImportError: ERROR: recursion is detected during loading of “cv2” binary extensions. Check OpenCV installation. 2. 解决 cv2版本太高,需要降低cv2版本 2.1 在anaconda环境下使用conda list查看当前cv2的版本为4.6.0.66,如下图: 2.2 使用pip uninstall opencv-python==4.6.0.66(指定卸载的当前cv2版本号)

    2024年02月12日
    浏览(55)
  • [保研/考研机试] KY110 Prime Number 上海交通大学复试上机题 C++实现

    Prime Number https://www.nowcoder.com/share/jump/437195121691717713466 Output the k-th prime number. 输入描述: k≤10000 输出描述: The k-th prime number. 输入: 输出:

    2024年02月13日
    浏览(24)
  • LeetCode447. Number of Boomerangs

    You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters). Return the number of boomerangs. Example 1: Input: points = [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs

    2024年02月02日
    浏览(24)
  • LeetCode 933. Number of Recent Calls

    ou have a  RecentCounter  class which counts the number of recent requests within a certain time frame. Implement the  RecentCounter  class: RecentCounter()  Initializes the counter with zero recent requests. int ping(int t)  Adds a new request at time  t , where  t  represents some time in milliseconds, and returns the number of requests that has h

    2024年02月08日
    浏览(36)
  • 算法 in Go:Binary Search(二分查找)

    猜数 1、2、3、4、5、6、7、8 排好序一个集合,先从中间开始猜,根据提示就可以排除一半,在剩余的一半里,再从中间开始猜,依此类推,这就是二分查找。 输入:排好序的集合 如果要查找的元素在集合中:返回位置(索引) 否则:返回空 如果查找? [1,2,3,4,5,...56,57,58..

    2024年02月08日
    浏览(27)
  • 【自监督论文阅读笔记】EVA: Exploring the Limits of Masked Visual Representation Learning at Scale

            本文推出了 EVA ,这是一个 以视觉为中心 的基础模型,旨在仅使用可公开访问的数据来 探索大规模 视觉表示的 局限性 。EVA 是一种经过预训练的普通 ViT,用于 重建 以可见图像块为条件的 屏蔽掉的 图像-文本对齐(image-text aligned)的视觉特征 。通过这个前置任

    2024年02月06日
    浏览(43)
  • LeetCode //C - 933. Number of Recent Calls

    You have a RecentCounter class which counts the number of recent requests within a certain time frame. Implement the RecentCounter class: RecentCounter() Initializes the counter with zero recent requests. int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the pas

    2024年01月23日
    浏览(36)
  • 【学习笔记】CF582D Number of Binominal Coefficients

    注意 P P P 事实上不会影响复杂度,因为关于组合数,我们有一个非常经典的结论: ( n + m m ) binom{n+m}{m} ( m n + m ​ ) 包含的 P P P 的幂的次数等于 n n n 和 m m m 在 P P P 进制下做加法的进位次数。这样,我们只需要关心进位的次数,而不必知道每一位具体是多少。 这个结论的证

    2024年02月12日
    浏览(22)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包