首页 > 其他分享 >leetcode-762-easy

leetcode-762-easy

时间:2023-03-31 20:14:21浏览次数:29  
标签:prime count set 762 int easy bits leetcode left

Prime Number of Set Bits in Binary Representation

Given two integers left and right, return the count of numbers in the inclusive range [left, right] 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 1's present when written in binary.

For example, 21 written in binary is 10101, which has 3 set bits.
Example 1:

Input: left = 6, right = 10
Output: 4
Explanation:
6  -> 110 (2 set bits, 2 is prime)
7  -> 111 (3 set bits, 3 is prime)
8  -> 1000 (1 set bit, 1 is not prime)
9  -> 1001 (2 set bits, 2 is prime)
10 -> 1010 (2 set bits, 2 is prime)
4 numbers have a prime number of set bits.
Example 2:

Input: left = 10, right = 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)
5 numbers have a prime number of set bits.
Constraints:

1 <= left <= right <= 106
0 <= right - left <= 104

思路一:暴力求解

    public int countPrimeSetBits(int left, int right) {
        int count = 0;
        for (int i = left; i <= right; i++) {
            if (isNumBitCountsPrime(i)) {
                count++;
            }
        }

        return count;
    }

    public boolean isNumBitCountsPrime(int num) {
        int count = 0;
        while (num > 0) {
            if ((num & 1) == 1) {
                count++;
            }
            num >>= 1;
        }

        if (count == 1) {
            return false;
        }

        for (int i = 2; i < count; i++) {
            if (count % i == 0) {
                return false;
            }
        }

        return true;
    }

标签:prime,count,set,762,int,easy,bits,leetcode,left
From: https://www.cnblogs.com/iyiluo/p/17277353.html

相关文章

  • Leetcode Practice --- 栈和队列
    目录155.最小栈思路解析20.有效的括号思路解析1047.删除字符串中的所有相邻重复项思路解析1209.删除字符串中的所有相邻重复项II思路解析删除字符串中出现次数>=2次的相邻字符剑指Offer09.用两个栈实现队列239.滑动窗口最大值思路解析155.最小栈设计一个支持push......
  • LeetCode 100 相同的树
    LeetCode|100.相同的树给你两棵二叉树的根节点p和q,编写一个函数来检验这两棵树是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。示例1:输入:p=[1,2,3],q=[1,2,3]输出:true示例2:输入:p=[1,2],q=[1,null,2]输出:false示例3:输入:p......
  • LeetCode 94 二叉树的中序遍历
    LeetCode|94.二叉树的中序遍历给定一个二叉树的根节点root,返回它的中序 遍历。示例1:输入:root=[1,null,2,3]输出:[1,3,2]示例2:输入:root=[]输出:[]示例3:输入:root=[1]输出:[1]提示:树中节点数目在范围[0,100]内-100<=Node.val<=100迭代实现:......
  • 双网卡设备通过HIKSDK接入EasyCVR平台显示离线是什么原因?
    EasyCVR视频融合平台基于云边端协同架构,具有强大的数据接入、处理及分发能力,平台支持海量视频汇聚管理,可支持多协议接入,包括市场主流标准协议与厂家私有协议及SDK,如:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、宇视SDK等(具体见下图)。平台能在复杂的网络环境中,将分散的各......
  • 华为NVR设备接入EasyCVR视频融合平台后不显示摄像头的问题排查与解决
    在上期的文章中,我们和大家分享了关于EasyCVR平台与华为IVS3800平台的对接相关经验分享,感兴趣的用户可以翻阅我们往期的文章进行查看。今天我们来分享一下华为NVR设备接入平台后不显示摄像头的问题排查与解决。在EasyCVR对接华为NVR设备的过程中,通常是使用国标GB28181协议,但是有......
  • AI视频智能分析平台EasyCVR设备录像功能细节优化
    EasyCVR视频融合平台基于云边端一体化架构,具有强大的数据接入、处理及分发能力,平台支持海量视频汇聚管理,可支持多协议接入,包括市场主流标准协议与厂家私有协议及SDK,如:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、宇视SDK等(具体见下图)。平台丰富的视频能力包括:视频监控直......
  • 反转链表-leetcode92
    给你单链表的头指针head和两个整数left和right,其中left<=right。请你反转从位置left到位置right的链表节点,返回反转后的链表。示例1:输入:head=[1,2,3,4,5],left=2,right=4输出:[1,4,3,2,5]示例2:输入:head=[5],left=1,right=1输出:[5]//leet......
  • Java中使用EasyExcel生成Excel文件
    使用Spring框架中的@ExcelProperty注解生成Excel文件需要借助于第三方库,比如EasyExcel或ApachePOI等。首先定义实体类,例如publicclassUser{@ExcelProperty(value="姓名",index=0)privateStringname;@ExcelProperty(value="年龄",index=1)priva......
  • Leetcode19. 删除链表的倒数第 N 个结点
     19. 删除链表的倒数第N个结点自己纯手写第一题,递归有点冗杂,开辟了虚拟头节点,而且要特别注意边界条件(当倒数第n个正好是头节点时)。***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),n......
  • LeetCode 559.二叉树的最大深度
    1.题目:给定一个N叉树,找到其最大深度。最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。N叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。示例1:输入:root=[1,null,3,2,4,null,5,6]输出:3来源:力扣(LeetCode)链接:https://leetcode.cn/problems/maximum......