public class Solution { public int countPrimes(int n) { boolean[] isDelArray = new boolean[n];

来源:互联网 发布:ubuntu 查看mysql 编辑:程序博客网 时间:2024/05/16 14:16

1.注意边界条件

public class Solution {public int countPrimes(int n) {boolean[] isDelArray = new boolean[n];if (n <= 2)return 0;isDelArray[2] = false;for (int i = 3; i < n; i++) {if (i % 2 == 0)isDelArray[i] = true;elseisDelArray[i] = false;}for (int i = 3; i < n; i += 2) {if (!isDelArray[i]) {if (i * i > n)break;for (int j = 2; i * j < n; j++) {isDelArray[i * j] = true;}}}int ans = 0;for (int i = 2; i < n; i++) {if (!isDelArray[i])ans++;}return ans;}}
参考:
1.http://blog.csdn.net/lisonglisonglisong/article/details/45309651

阅读全文
0 0
原创粉丝点击