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

leetcode 204. Count Primes

时间:2017-06-02 13:31:11      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:width   return   leetcode   count   mem   etc   logs   set   标记   

题目描述

技术分享

 

注意: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

标签:width   return   leetcode   count   mem   etc   logs   set   标记   

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

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