Count the number of prime numbers less than a non-negative number, n
public int countPrimes(int n) { ArrayList<Integer> primes = new ArrayList<Integer>(); if (n <= ) return Math.max(, n - ); primes.add();
primes.add(); for (int i = ; i <= n; i++) {
boolean isPrime = true;
for (int p : primes) {
int m = i % p;
if (m == ) {
isPrime = false;
break;
}
} if (isPrime) {
primes.add(i);
}
} return primes.size();
}
class Solution {
public int countPrimes(int n) {
boolean[] notPrime = new boolean[n];
int count = ;
for (int i = ; i < n; i++) {
if (notPrime[i] == false) {
count++;
for (int j = ; i * j < n; j++) {
notPrime[i * j] = true;
}
}
}
return count;
}
}
Count Primes的更多相关文章
-
[leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
-
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 ...
-
HDU 5901 Count primes 论文题
Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...
-
[leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
-
hdu 5901 Count primes (meisell-Lehmer)
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
-
LeetCode_204. Count Primes
204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...
-
[LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
-
Count Primes - LeetCode
examination questions Description: Count the number of prime numbers less than a non-negative number ...
-
【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
随机推荐
-
java——IO流
一. File File类可以使用文件路径字符串来创建File实例,该文件路径可以是绝对路径或相对路径 File类的list()方法中可以接收一个FilenameFilter参数,通过该参数可以只列出 ...
-
decimal(a,b)
decimal(a,b)a指定指定小数点左边和右边可以存储的十进制数字的最大个数,最大精度38.b指定小数点右边可以存储的十进制数字的最大个数.小数位数必须是从 0 到 a之间的值.默认小数位数是 0 ...
-
Selenium2+python自动化2-pip降级selenium3.0
selenium版本安装后启动Firefox出现异常:'geckodriver' executable needs to be in PATH selenium默默的升级到了3.0,然而网上的教程都是 ...
-
[LA3026]Period
[LA3026]Period 试题描述 For each prefix of a given string S with N characters (each character has an ASC ...
-
[Hive - Tutorial] Creating, Showing, Altering, and Dropping Tables
Creating, Showing, Altering, and Dropping Tables See Hive Data Definition Language for detailed info ...
-
poj 2001 Shortest Prefixes
字典树的简单应用. #include<stdio.h> #include<string.h> ][]; struct node{ int cnt; node *next[]; ...
-
wpa_cli P2P 连接相关的调试命令
在最近的一次客户端上的调试p2p的wifi display, 他们中的一半Android该调整了,整个前所以没有太多的研究p2p过程连接, 客户现在使用的非Android平台架构. 需要协助客户这么多 ...
-
2.sass变量、嵌套、混合(mixin)、继承拓展、@import、comment
变量.嵌套.混合(mixin).继承拓展.@import.comment 变量的意义 在sass里我们可以定义多个变量来存放颜色.边框等等的样式,这样就可以在下面想要使用样式的时候使用变量了 这样的优 ...
-
响应式编程知多少 | Rx.NET 了解下
1. 引言 An API for asynchronous programming with observable streams. ReactiveX is a combination of the ...
-
MacOS 的预览 Preview 打开pdf 容易卡死 解决方案
MacOs 10.13.6 打开pdf之后容易卡死. 移动一下窗口之后就卡死了. 有时候等一会还能缓过来,有时候就缓不过来了. 只要执行下这个命令就可以了. sudo rm -rf ~/Library ...