作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/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.)

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:

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

题目大意

判断在[L, R]闭区间内,有多少数字的二进制表示中1的个数是质数。

解题方法

遍历数字+质数判断

理解题意之后很简单,要判断所有介于L 和 R之间的数的二进制表示中1的个数是质数的个数。

分解为以下几步:

  1. 找到所有介于L 和 R之间的数的二进制表示
  2. 判断每个二进制数表示中1的个数是否为质数
  3. 求为质数的个数

很容易想到如果是质数返回1否则为0,使用sum()函数即可求得为质数的个数。

import math
class Solution(object):
def isPrime(self, num):
if num == 1:
return 0
elif num == 2:
return 1
for i in xrange(2, int(math.sqrt(num))+1):
if num % i == 0:
return 0
return 1
def countPrimeSetBits(self, L, R):
"""
:type L: int
:type R: int
:rtype: int
"""
return sum(self.isPrime(bin(num)[2:].count('1')) for num in xrange(L, R+1))

精简了代码,两行就能搞定。

class Solution(object):
def countPrimeSetBits(self, L, R):
isPrime = lambda num : 0 if ((num == 1) or (num % 2 == 0 and num > 2)) else int(all(num % i for i in xrange(3, int(num ** 0.5) + 1, 2)))
return sum(isPrime(bin(num)[2:].count('1')) for num in xrange(L, R+1))

一个数的二进制表示最多只有32位,所以直接保存32以内的质数进行查找判断即可。空间换时间的一个思路。

class Solution(object):
def countPrimeSetBits(self, L, R):
"""
:type L: int
:type R: int
:rtype: int
"""
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
res = 0
for num in range(L, R + 1):
if bin(num).count("1") in primes:
res += 1
return res

日期

2018 年 1 月 17 日
2018 年 11 月 9 日 —— 睡眠可以

【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)的更多相关文章

  1. 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 ...

  2. Leetcode 762. Prime Number of Set Bits in Binary Representation

    思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...

  3. 【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 ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. 762. Prime Number of Set Bits in Binary Representation

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. docker 使用加速器下载

    因为docker官网的镜像地址docker.hum.com是在国外的 所以下载速度比较慢,国内有一些镜像源是比较快的,内容是和docker官网的一致 常用的加速器有 docker-cn 阿里云加速器 ...

  2. java输入代码

    import java.util.Scanner; public class Demo59 {    public static void main(String[] args) {        / ...

  3. binlog真的是银弹吗?有些时候也让人头疼

    大家好,我是架构摆渡人.这是实践经验系列的第三篇文章,这个系列会给大家分享很多在实际工作中有用的经验,如果有收获,还请分享给更多的朋友. binlog 用于记录用户对数据库操作的SQL语句信息,同时主 ...

  4. acid, acknowledge, acquaint

    acid sulphuric|hydrochloric|nitric|carbolic|citric|lactic|nucleic|amino acid: 硫|盐|硝|碳|柠檬|乳|核|氨基酸 王水是 ...

  5. Angular @ViewChild,Angular 中的 dom 操作

    Angular 中的 dom 操作(原生 js) ngAfterViewInit(){ var boxDom:any=document.getElementById('box'); boxDom.st ...

  6. Vue相关,diff算法。

    1. 当数据发生变化时,vue是怎么更新节点的? 要知道渲染真实DOM的开销是很大的,比如有时候我们修改了某个数据,如果直接渲染到真实dom上会引起整个dom树的重绘和重排,有没有可能我们只更新我们修 ...

  7. HongYun-ui搭建记录

    vue项目windows环境初始化 Element-ui使用 vue2 页面路由 vue SCSS 在VUE项目中使用SCSS ,对SCSS的理解和使用(简单明了) vue axios vue coo ...

  8. poi做一个简单的EXCAL

    //创建一个实体类 package text; import java.util.Date; public class Student { private int id; private String ...

  9. nvm命令

    1.安装node nvm install node版本 2.查看已安装版本 nvm list 3.切换node版本 nvm use node版本 4.查看版本 node -v

  10. Python模块和函数

    目录 一.基础 二.特殊函数 一.基础 #导入模块 import xxx #调用 xxx.dd() from xxx import xx  as dd #导入某个函数,as给函数加别名,调用xx() ...