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. 02.photoshop制作透明图片步骤

    1.首先打开PS软件和图片如下.文件-新建. 2.如下图背景内容选择-透明.在确定. 3.出现了白底这就是透明的. 4.将图片背景都去除 5.在选择文件-存储为 6.格式选择.GIF的文件.其它不是透 ...

  2. js 监听组合键盘事件

    有些时候,我们需要在网页上,增加一些快捷按键,方便用户使用一些常用的操作,比如:保存,撤销,复制.粘贴等等. 我们所熟悉的按键有这么集中类型: 单独的按键操作,如:delete.up.down等 两位 ...

  3. day01-MySQL介绍

    一.MySQL的介绍 1.1.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 W ...

  4. canal 入门

    参考文章:Canal - 安装   https://www.aliyun.com/jiaocheng/1131288.html?spm=5176.100033.2.7.7b422237XAirIe 前 ...

  5. flash推流工具<转>

    https://github.com/young-cowboy/young-cowboy.github.io https://www.cnblogs.com/xiaoniuzai/p/7129036. ...

  6. vue 设置背景

    <span :style="{ 'background': 'url(' + aboutImg1 + ') no-repeat center center', 'background- ...

  7. C语言复习:结构体

    结构体专题 01.结构体类型定义及结构体变量定义     char c1,char c2, char name[62]; int age     char name[62]; int age,char ...

  8. Zabbix邮件报警配置

    一.安装sendmail或者postfix yum install sendmail #安装 service sendmail start #启动 chkconfig sendmail on #设置开 ...

  9. PL/SQL编码规范的一些建议

    由于业务的复杂多变,我们编写完的程序,在后期肯定要被修改,而且修改的人很可能不是自己.这种情况我们都遇到过. 而且,看别人的代码可能会觉得很痛苦:为什么他要这样写相关逻辑?为什么变量名称要这样定义?换 ...

  10. SQL Server 中BIT类型字段增删查改那点事

    话说BIT类型字段之前,先看“诡异”的一幕,执行Update成功,但是查询出来的结果依然是1,而不是Update的2 当别人问起我来的时候,本人当时也是处于懵逼状态的,后面联想具体的业务突然想起来这个 ...