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. [leetcode] Count Primes

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

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

  3. HDU 5901 Count primes 论文题

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

  4. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

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

  5. hdu 5901 Count primes (meisell-Lehmer)

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

  6. LeetCode_204. Count Primes

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

  7. 【刷题-LeetCode】204. Count Primes

    Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...

  8. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

  9. [LeetCode] Count Primes 质数的个数

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

  10. Count Primes - LeetCode

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

随机推荐

  1. iOS运行时 -- Runtime(摘抄自网络)

    运行时(iOS) 一.什么是运行时(Runtime)? 运行时是苹果提供的纯C语言的开发库(运行时是一种非常牛逼.开发中经常用到的底层技术) 二.运行时的作用? 能获得某个类的所有成员变量 能获得某个 ...

  2. Gson 的使用

    Gson 的使用简介Gson 是 Google 开发的Java API,用于转换Java对象 和 Json 对象. 使用Maven 依赖<dependency> <groupId&g ...

  3. SVN 服务器端安装过程

    1.安装软件版本: VisualSVN-Server-2.1.5.msi 右击安装软件,单机“安装” 2.单击[Next] 选择“I accept the terms in the License A ...

  4. BZOJ 1066 POJ 2711 [SCOI2007]蜥蜴

    与POJ 1815 Friendship类似,该题之前也做过 目前处于TLE状态.样例已经通过 1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec  Memory Limit:  ...

  5. [IOS UICollectionView模版]

    创建CollectionCell模版: 1.新建类CollectionCell继承自UICollectionViewCell 2.新建Xib,命名为CollectionCell.xib a.选中Col ...

  6. 新建的 web 工程 有红色的惊叹号

    新建的 web 工程 有红色的感叹号问题:     在eclipse 中新建一个web工程,但是工程上有红色的感叹号.解决:     1.右键工程,选择Build Path-->Configur ...

  7. hdu acmsteps 2.2.1 Fibonacci

    Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. codeforce626C.Block Towers(二分)

    C. Block Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. mysql 服务无法启动 服务没有报告任何错误

    问题 解决方法 1.必须保证 mysql 下不存在 data 文件夹,如果存在 data 文件夹,则先删除 mysql 下的 data 文件夹,然后初始化 mysqld --initialize 服务 ...

  10. zabbix: failed to accept an incoming connection

    错误描述 查日志发现: failed to accept an incoming connection: connection from "192.168.186.132" rej ...