首页 > 其他 > 详细

leetcode 204. Count Primes

时间:2017-06-02 13:20:58      阅读:292      评论:0      收藏:0      [点我收藏+]

题目描述

技术分享

 

注意:1既不是素数也不是合数

可以通过标记的方法找到所有非素数。

技术分享

class Solution {
public:
    int countPrimes(int n) {
        int *tmp = new int[n];
        memset(tmp,0,sizeof(int)*n);
        int count = 0;
        for(int i = 2; i < n ; i++){
            if(tmp[i] == 0){
                count ++;
            for(int j = 2; j*i < n; j++)
                tmp[i*j]=1;
            }
        }
        return count;
    }
};

 

leetcode 204. Count Primes

原文:http://www.cnblogs.com/strongYaYa/p/6932701.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!