[LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation
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 1
s present when written in binary. For example, 21
written in binary is 10101
which has 3 set bits. Also, 1 is not a prime.)
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)
Note:
L, R
will be integersL <= R
in the range[1, 10^6]
.R - L
will be at most 10000.
class Solution:
def countPrimeSetBits(self, L, R):
"""
:type L: int
:type R: int
:rtype: int
"""
def is_prime(a):
if a<=1:
return False
i=2
while i*i<=a:
if a%i==0:
return False
i+=1
return True ans=0
for i in range(L,R+1):
b=bin(i)[2:]
if is_prime(b.count('1')):
ans+=1 return ans
[LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation的更多相关文章
- 【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation
problem 762. Prime Number of Set Bits in Binary Representation solution1: class Solution { public: i ...
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告
题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
[抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- Leetcode 762. Prime Number of Set Bits in Binary Representation
思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...
- 762. Prime Number of Set Bits in Binary Representation
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
随机推荐
- Android 编码规范
1.文件编码:UTF-8 源文件编码格式为 UTF-8. 2.数组初始化:可写成块状结构 数组初始化可以写成块状结构,比如,下面的写法都是OK的: new int[] { 0, 1, 2, 3 } n ...
- CSS3 的box-shadow进阶之 - 基础知识篇
box-shadow被认为是CSS3最好的特性之一,发挥想象力,搭配其它属性,可以做出很多好看的效果(如下图,将会放在下一篇文章讲解),这篇文章主要讲一下box-shadow的基础知识. ...
- Oracle.练习题
2018-07-31 ---练习3 ---创建sporter表 create table sporter( sporterid ) constraint sport_id primary key, s ...
- 逆袭之旅DAY28.XIA.异常处理
2018-07-24 14:42:24 第一种: 第二种: 第三种: 执行 try--catch--finally--return(执行return 退出方法) 代码示例: 输入数字,输出对应课 ...
- GitHub下的文件放到Linux系统下
1.在GitHub账号下clone URL 项目. 2.到Linux服务器下执行以下操作: (1) mkdir test (2) cd test/ (3) git clone 复制的项目URL
- centos6.5 安装PHP7.0支持nginx
1.安装PHP所需要的扩展 yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel ...
- css3 居中(推荐弹性盒模型方式)
参考 http://www.zhihu.com/question/20774514 http://caibaojian.com/demo/flexbox/align-items.html 例子:ht ...
- 【阅读笔记】《C程序员 从校园到职场》第三章 程序的样式(大括号)
参考: https://blog.csdn.net/zhouzhaoxiong1227/article/details/22820533 一..初始化数组变量 在实际的软件开发项目中,变量在使用前应初 ...
- 查找xml中的接口名及涉及表名并输出
#! /usr/bin/env python3 # -*- coding:utf-8 -*- import xml.dom.minidom #该模块被用来处理xml文件 import re #正则表 ...
- eclipse搭建ssm框架
新建数据库ssm 建立数据库表user CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT , `sex` varchar(255) ...