[LeetCode]Count Primes

来源:互联网 发布:体感游戏机的价格淘宝 编辑:程序博客网 时间:2024/05/07 14:39

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 int countPrimes(int n) {boolean[] flag = new boolean[n];int count=0;for(int i=2; i<n; i++){if(flag[i] == true)continue;count++;for(int j=i; j<n; j+=i)       flag[j] = true;}return count;}

0 0
原创粉丝点击