204. Count Primes

来源:互联网 发布:淘宝买家怎么删除追评 编辑:程序博客网 时间:2024/05/22 07:00

Description:

Count the number of prime numbers less than a non-negative number, n.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

数质数

public class Solution {    public int countPrimes(int n) {        if(n<2)return 0;        int [] list = new int [n];        list[0]=1;        list[1]=1;        for(int i=2;i<n/2+1;i++){            if(list[i]==1)continue;            int j =2;            while(i*j<n){                list[i*j]=1;                j++;            }        }        int c = 0;        for(int i=2;i<n;i++){            if(list[i]==0)c++;        }        return c;    }}

0 0
原创粉丝点击