leetcode 204. Count Primes

題目描述

 

注意:1既不是素數也不是合數spa

能夠經過標記的方法找到全部非素數。code

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;
    }
};
相關文章
相關標籤/搜索