码迷,mamicode.com
首页 > 其他好文 > 详细

204. Count Primes

时间:2016-06-09 12:17:44      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     * 204. Count Primes
     * 2016-6-8 by Mingyang
     * 这个题目非常的巧妙,首先从第一个数2开始,所有2的倍数全部变为true
     * 然后继续下一个数,每一个数开始把自己的倍数全部化为true
     */
     public int countPrimes(int n) {
            boolean[] notPrime = new boolean[n];
            int count = 0;
            for (int i = 2; i < n; i++) {
                if (notPrime[i] == false) {
                    count++;
                    for (int j = 2; i*j < n; j++) {
                        notPrime[i*j] = true;
                    }
                }
            }
            return count;
        }

 

204. Count Primes

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5572117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!