国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LeetCode 204. Count Primes

2019-11-14 13:04:47
字體:
供稿:網(wǎng)友

解題思路:維基百科中 https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 

 Input: an integer n > 1.  Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true.  for i = 2, 3, 4, ..., not exceeding √n:   if A[i] is true:     for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n:       A[j] := false.  Output: all i such that A[i] is true.

class Solution {public:        int countPRimes(int n) {            if (2 >= n) return 0;            vector<bool>primes(n,true);            int sqr = sqrt(n - 1);            for (int i = 2; i <= sqr; ++i){                if (primes[i]){                    for (int j = i * i; j < n; j += i)                    primes[j] = false;            }            }            int sum = 0;            for (int i = 2; i < n; ++i)                sum += (primes[i]) ? 1 : 0;            return sum;    }};


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 勃利县| 定远县| 岗巴县| 开原市| 邛崃市| 团风县| 永城市| 本溪| 雅安市| 盐池县| 龙井市| 曲沃县| 马关县| 兴海县| 神农架林区| 麦盖提县| 麻江县| 巴彦县| 余姚市| 西青区| 库车县| 武川县| 江津市| 仙桃市| 哈尔滨市| 手游| 民县| 琼结县| 扎兰屯市| 留坝县| 榆树市| 乐昌市| 高清| 乾安县| 汶川县| 盱眙县| 香格里拉县| 湘阴县| 木里| 玉山县| 开鲁县|