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 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)
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么统计二进制数量:右移1就行了。
质数就那么些,枚举就行了。存不存在都放在set里。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
往右移一位要配合&1,才能取出最后一位。
for (int i = num; i > 0; i >>= 1)
bits += i & 1;
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
质数可以单独拿出来做成set
Set<Integer> set = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
//L = 6, R = 10
class Solution {
public int countPrimeSetBits(int L, int R) {
//corner case
if (L == R) return 0; //initialization:set
Set<Integer> set = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));
int count = 0; //for loop from l to right, add to count
for (int num = L; num <= R; num++) {
//count the bits
int bits = 0;
for (int i = num; i > 0; i >>= 1)
bits += i & 1;
count = set.contains(bits) ? count + 1 : count;
} return count;
}
}
762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量的更多相关文章
- 【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 解题报告
题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- [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 ...
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- [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 ...
- 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 ...
- Leetcode762.Prime Number of Set Bits in Binary Representation二进制表示中质数个计算置位
给定两个整数 L 和 R ,找到闭区间 [L, R] 范围内,计算置位位数为质数的整数个数. (注意,计算置位代表二进制表示中1的个数.例如 21 的二进制表示 10101 有 3 个计算置位.还有, ...
- [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 ...
随机推荐
- Java StringBuffer和StringBuilder类
Java StringBuffer和StringBuilder类 当对字符串进行修改的时候,需要使用StringBuffer和StringBuilder类. 和String类不同的是,StringBu ...
- zombodb 得分以及高光
得分以及高光在搜索引擎中有很重要的作用 得分zdb.score 使用方法 zdb.score(tid) 参考示例 SELECT zdb.score(ctid), * FROM products WHE ...
- Javascript var 和 let 的区别
Javascript var 和 let 的区别 var 是函数块的全局变量. let 是代码块的局部变量. let 变量不会提升,如果先使用后定义会 undefind. 参考: https://de ...
- golang http proxy反向代理
本文介绍golang中如何进行反向代理. 下面例子中, proxy server接收client 的 http request,转发给true server,并把 true server的返回结果再发 ...
- TreeSet的两种排序方式,含Comparable、Comparator
1.排序的引入 由于TreeSet可以实现对元素按照某种规则进行排序,例如下面的例子 public class TreeSetDemo { public static void main(String ...
- hash 在 perl 中的用法(转载)
Perl的数据结构中最有趣的一个特性是哈希(hash),它使得在数据片段之间建立键-值(key-value)关联成为可能.虽然这些哈希要远远比普通系统中以数字索引的数组用途更广,但是往往也会使初学者不 ...
- 知识点:Java 集合框架图
知识点:Java 集合框架图 总结:Java 集合进阶精讲1 总结:Java 集合进阶精讲2-ArrayList Java集合框架图 我们经常使用的Arrayist.LinkedList继承的关系挺复 ...
- 使用Mechanize实现自动化表单处理
使用Mechanize实现自动化表单处理 mechanize是对urllib2的部分功能的替换,能够更好的模拟浏览器行为,在web访问控制方面做得更全面 mechanize的特点: 1 http, ...
- VS 代码片段集
片段1:用于线程执行代码,耗时操作时加载Loging; <?xml version="1.0" encoding="utf-8"?> <Cod ...
- dubbo使用简介
---------------------------------------------------------------------------------------------------- ...