204. Count Primes 统计<n的质数的个数
[抄题]:
Count the number of prime numbers less than a non-negative number, n.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
质数的倍数是合数
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
质数的倍数是合数
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int countPrimes(int n) {
//cc
if (n < 2) {
return 0;
} //ini
int count = 0;//prime num
int[] notPrime = new int[n];//not prime num //for i, i * j
for (int i = 2; i < n; i++) {
if (notPrime[i] == 0) count++;
for (int j = 2; i * j < n; j++) {
notPrime[i * j] = 1;
}
} return count;
}
}
204. Count Primes 统计<n的质数的个数的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- 204. Count Primes - LeetCode
Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- 【LeetCode】 204. Count Primes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
- [LeetCode] 204. Count Primes 解题思路
Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- LeetCode 204. Count Primes计数质数 (C++)
题目: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
随机推荐
- c++primer 第二章编程练习答案
2.7.1 #include<iostream> int main() { using namespace std; ]; ]; cout << "input nam ...
- 机器学习:YOLO for Object Detection (二)
之前介绍了 YOLO-v1 单纯的利用一个卷积网络完成了目标检测,不过 YOLO-v1 虽然速度很快,但是比起其他的网络比如 Fast R-CNN 检测的准确率还是差不少,所以作者又提出了改良版的 Y ...
- main函数的参数的用法
说明:main函数的参数的用法源代码: #include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[] ...
- THUPC2017 小 L 的计算题
求 $k=1,2,\cdots,n \space \space \sum\limits_{i=1}^n a_i^k$ $n \leq 2 \times 10^5$ sol: 时隔多年终于卡过去了 之前 ...
- 1117. Eddington Number(25)
British astronomer Eddington liked to ride a bike. It is said that in order to show off his skill, h ...
- LeetCode 369. Plus One Linked List
原题链接在这里:https://leetcode.com/problems/plus-one-linked-list/ 题目: Given a non-negative number represen ...
- UV有问题?
1.检查读取显示贴图的环境与制作贴图环境UV坐标系是否一致. 如:Directx左上角(0,0),右下角(1,1) unity 左下角(0,0),右上角(1,1) 两者互转需要垂直镜像.
- IntellJ IDEA快捷键汇总
今天开始使用IDEA,各种不习惯,一会Eclipse一会IDEA来回切换,需要一个熟悉的过程,记录一下常用的快捷键. IDEA常用快捷键 Alt+回车 导入包,自动修正Ctrl+N 查找类Ctrl+ ...
- C的随想
c用的是操作系统函数,这个一下子就限制了APi的数量,通过组合这些系统api即可实现功能. c开发的人一般都会熟记系统函数,然后需要确定函数参数的时候,通过man指令进行查看 对于32位64位将会导致 ...
- Filebeat 5.x 日志收集器 安装和配置
Filebeat 5.x版本 风来了.fox 1.下载和安装 https://www.elastic.co/downloads/beats/filebeat 这里选择 LINUX 64-BIT 即方式 ...