题意:统计小于n的质数个数。

作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法。

是的,素数筛法并不是该题最佳的解法,线性素数筛法才是。

至于什么是素数筛法,请百度吧。

 class Solution {
public:
int countPrimes(int n) {
bool *isp= new bool[n];
for (int i = ; i < n; ++i)
{
isp[i]= true;
}
for (int i = ; i * i< n; ++i)//素数筛法
{
if(isp[i]){
for(int j = i + i; j < n; j+=i){
isp[j] = false;
}
}
}
int ans = ;
for (int i = ; i < n; ++i){
ans += isp[i];
}
delete isp;
return ans;
}
};

Leetcode 204 Count Primes 数论的更多相关文章

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

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

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

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

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

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

  4. Java [Leetcode 204]Count Primes

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

  5. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  6. LeetCode 204 Count Primes

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

  7. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  8. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

  9. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

随机推荐

  1. 前端---HTML

    HTML基础 本章内容: 简介 HTML定义 标签定义和属性 HTML5基本结构 HTML5字符集 <head>标签 <title> <base/> <lin ...

  2. golang使用interface来mock进行测试(来自dotGO2014)

    源自于dotGO 2014的视频,讲述如何使用 interface 来mock 进行测试.. 可以FQ的同学自己观看,这里把重要的一些代码给截图搬到国内了 https://www.youtube.co ...

  3. HttpURLConnection请求网络数据的Post请求

    //--------全局变量----------- //注册Url    private String urlPath="http://101.200.142.201:8080/VideoP ...

  4. zendstudio 声明变量类型,让变量自动方法提示

    zendstudio 行内注释, 显式声明变量类型,让变量自动方法提示 $out = []; /* @var $row \xxyy\SizeEntity */ foreach ($rows[ 'lis ...

  5. 用sql从一张表更新数据到另外一张表(多表数据迁移)

    update TBL_1 A, TBL_2 B, TBL_3 Cset a.email=c.email_addrwhere a.user_id=b.user_id and b.un_id=c.un_i ...

  6. Mac 系统下类似于 apt-get 的软件包管理器 -- Homebrew

    对于一个习惯了在 Ubuntu 的终端上通过 apt-get 来安装工具软件的我来说,也希望在Mac上找到类似的工具,能很方便的一条命令就能安装所需的软件,而不用手工的去查找下载编译,或者是折腾安装所 ...

  7. MVC过滤器

    MVC的每一个请求都会给相应的控制器的对应行为方法处理,那么想在这些处理的前 前后后增加一些额外的逻辑处理,因此过滤器的作用就来了 MVC支持的过滤器类型有四种,分别是:Authorization(授 ...

  8. ubuntu 14.04 键盘快捷键显示

    安装完ubuntu14.04后 第一次登陆,会弹出键盘快捷键的一个大体预览, 也没有一个关闭按钮, 没看完就自动消失了. 让其重现的方法: 按住 super键  ,也就是键盘上的  win键 . 键盘 ...

  9. Java里的构造函数(构造方法)

    构造函数 ,是一种特殊的方法.主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类型 ...

  10. 人脸识别经典算法二:LBP方法

    与第一篇博文特征脸方法不同,LBP(Local Binary Patterns,局部二值模式)是提取局部特征作为判别依据的.LBP方法显著的优点是对光照不敏感,但是依然没有解决姿态和表情的问题.不过相 ...