Count the number of prime numbers less than a non-negative number, n.

思路:寻找质数的方法

class Solution {
public:
int countPrimes(int n) {
int num = ;
if(n < ) return num; int i, j, index;
isPrime = new bool [n];
isPrime[]=false;
isPrime[]=false;
for(i = ; i < n; i++){
isPrime[i] = true;//initialize as true, means all are primes
} for(i = ; i < n; i++){
if(!isPrime[i]) continue; num++;
for(j=; i*j < n; j++){
isPrime[i*j] = false;
}
} return num;
}
private:
bool* isPrime;
};

204. Count Primes (Integer)的更多相关文章

  1. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  2. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  3. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

  4. 【刷题-LeetCode】204. Count Primes

    Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...

  5. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  6. [LeetCode] 204. Count Primes 计数质数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  7. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

  8. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  9. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

随机推荐

  1. JS 事件 Event

    注册事件 target.addEventListener(type, listener, options); 或者 target.addEventListener(type, listener, us ...

  2. word中怎样设置页码包含总页数

    一个同事做毕业论文,论文是Word格式,1-2页是封面和目录,不需要页码,第3-10页是论文内容,需要从第1页开始显示,并显示论文内容的总页数8 页.具体为页脚处显示“第*页共*页”.他让我帮忙设置一 ...

  3. 根据svm将视频帧转换为img

    # -*- coding: utf-8 -*- """ Created on Mon Oct 1 09:32:37 2018 @author: Manuel " ...

  4. netty ChannelOption

    项目中用到很多netty,配置了各种不同的ChannelOption优化项,不同的配置对于在高并发情况下的性能有不小的影响 首先看下全部项目,参考下这篇文章,虽然不全 https://www.cnbl ...

  5. css:清楚浮动

    这个清楚浮动的方法最常用,给浮动字元素的父盒子,也就是不浮动元素,添加一个lhearfix的类,其类的css样式为: .clearfix:after{ /*必须要写这三句话*/ content:''; ...

  6. php版本升级导致openssl无法使用

    也就是call to undefined function openssl错误: 把extension前面的注释去掉,甚至把“libeay32.dll和ssleay32.dll文件复制并替换到apac ...

  7. Haskell语言学习笔记(92)HXT

    HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...

  8. SQL Server--存在则更新问题

    在博客园看到一篇讨论特别多的文章“探讨SQL Server并发处理存在就更新七种解决方案”,这种业务需求很常见:如果记录存在就更新,不存在就插入. 最常见的做法: BEGIN TRANSACTION ...

  9. 网络层——IP报文头介绍

    IP数据包也叫IP报文分组,传输在ISO网络7层结构中的网络层,它由IP报文头和IP报文用户数据组成,IP报文头的长度一般在20到60个字节之间,而一个IP分组的最大长度则不能超过65535个字节.  ...

  10. 在Unity中使用Lua脚本

    前言:为什么要用Lua首先要说,所有编程语言里面,我最喜欢的还是C#,VisualStudio+C#,只能说太舒服了.所以说,为什么非要在unity里面用Lua呢?可能主要是闲的蛋疼.....另外还有 ...