Count Primes

时间:2021-08-07 23:18:58

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的更多相关文章

  1. &lbrack;leetcode&rsqb; Count Primes

    Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...

  2. leetcode 263&period; Ugly Number 、264&period; Ugly Number II 、313&period; Super Ugly Number 、204&period; Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  3. HDU 5901 Count primes 论文题

    Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...

  4. &lbrack;leetcode&rsqb; 204&period; Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  5. hdu 5901 Count primes &lpar;meisell-Lehmer&rpar;

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  6. LeetCode&lowbar;204&period; Count Primes

    204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...

  7. &lbrack;LeetCode&rsqb; Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  8. Count Primes - LeetCode

    examination questions Description: Count the number of prime numbers less than a non-negative number ...

  9. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

随机推荐

  1. java——IO流

    一. File File类可以使用文件路径字符串来创建File实例,该文件路径可以是绝对路径或相对路径 File类的list()方法中可以接收一个FilenameFilter参数,通过该参数可以只列出 ...

  2. decimal&lpar;a&comma;b&rpar;

    decimal(a,b)a指定指定小数点左边和右边可以存储的十进制数字的最大个数,最大精度38.b指定小数点右边可以存储的十进制数字的最大个数.小数位数必须是从 0 到 a之间的值.默认小数位数是 0 ...

  3. Selenium2&plus;python自动化2-pip降级selenium3&period;0

    selenium版本安装后启动Firefox出现异常:'geckodriver' executable needs to be in PATH selenium默默的升级到了3.0,然而网上的教程都是 ...

  4. &lbrack;LA3026&rsqb;Period

    [LA3026]Period 试题描述 For each prefix of a given string S with N characters (each character has an ASC ...

  5. &lbrack;Hive - Tutorial&rsqb; Creating&comma; Showing&comma; Altering&comma; and Dropping Tables

    Creating, Showing, Altering, and Dropping Tables See Hive Data Definition Language for detailed info ...

  6. poj 2001 Shortest Prefixes

    字典树的简单应用. #include<stdio.h> #include<string.h> ][]; struct node{ int cnt; node *next[]; ...

  7. wpa&lowbar;cli P2P 连接相关的调试命令

    在最近的一次客户端上的调试p2p的wifi display, 他们中的一半Android该调整了,整个前所以没有太多的研究p2p过程连接, 客户现在使用的非Android平台架构. 需要协助客户这么多 ...

  8. 2&period;sass变量、嵌套、混合(mixin)、继承拓展、&commat;import、comment

    变量.嵌套.混合(mixin).继承拓展.@import.comment 变量的意义 在sass里我们可以定义多个变量来存放颜色.边框等等的样式,这样就可以在下面想要使用样式的时候使用变量了 这样的优 ...

  9. 响应式编程知多少 &vert; Rx&period;NET 了解下

    1. 引言 An API for asynchronous programming with observable streams. ReactiveX is a combination of the ...

  10. MacOS 的预览 Preview 打开pdf 容易卡死 解决方案

    MacOs 10.13.6 打开pdf之后容易卡死. 移动一下窗口之后就卡死了. 有时候等一会还能缓过来,有时候就缓不过来了. 只要执行下这个命令就可以了. sudo rm -rf ~/Library ...