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. 使用nginx绑定域名,代理gitlab

    默认情况下,gitlab通过自带的unicorn来充当web页面的,不用nginx也可以,这里我们使用nginx代理vim /etc/yum.reos.d/nginx.repo # 编辑nginx.r ...

  2. [转]Oracle中的索引详解

    原文地址:http://www.oschina.net/question/30362_4057 一. ROWID的概念 存储了row在数据文件中的具体位置:64位 编码的数据,A-Z, a-z, 0- ...

  3. [转] 再探java基础——break和continue的用法

    原文地址:http://blog.csdn.net/luoweifu/article/details/10756017 break break可用于循环和switch...case...语句中. 用于 ...

  4. Java-maven异常-cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突

    使用maven开发web应用程序, 启动的时候报错: jar not loaded. See Servlet Spec . Offending class: javax/servlet/Servlet ...

  5. BIEE连接数据库的方法

    BI创建(数据)分析.仪表盘.报表前,都需要对数据进行建模,在oracle biee里称为创建“资料档案库”-该文件后缀为RPD,所以一般也称为创建RPD文件. 步骤: 1.从windows开始菜单里 ...

  6. POJ1364 King

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...

  7. Eclipse启动Tomcat错误:Several ports (8005,8009) required by Tomcat v6.0 Server at localhost are already

    解决办法: 1.netstat -aon|findstr 8005 可查看指定端口号使用情况 2.tasklist |findstr 10452 找出占用指定进程Id的程序 3.taskkill /p ...

  8. UVA 1398 Meteor

    传送门 Solution: 记一颗流星在视野内的时间段为(L, R), 为了使所有(L, R)都取整数,首先将坐标放大. 放大倍数可取为 LCM(1, 2, ..., 10)= 2520 接着计算:从 ...

  9. TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133

    程序代码  class Person:      #constructor      def __init__(self,name,sex):           self.Name = name   ...

  10. 2层Xml读取类

    配置文件 <?xml> <root> <parent name="C"> <child name="C1">Sp ...