题意:统计小于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. ajax 调用 JSON.parse();

    $.ajax({           type : "POST",           data:{            createStartTime:createStartT ...

  2. UIButton的常用属性

    可以通过代码的方式创建UIButton 通用实例化对象方法: UIButton *button = [[UIButton alloc] initWithFrame:rect]; 快速实例化对象方法: ...

  3. Java中字符串比较时==和equals的区别

    ==是比较两个字符串引用的地址是否相同,即是否指向同一个对象,而equals方法则比较字符串的内容是否相同. 例如String a = "abc"; String b = &quo ...

  4. EnterpriseLibrary 6.0中DAAB独立数据库配置文件初始化

     string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "database.config");  IC ...

  5. Scrum Meeting (Oct. 27 2014)

    软件工程是一门十分有意思的课程,它不仅锻炼了我们开发软件的能力,更是给了我们结队作业的机会,在团队协作中,我们学会了欣赏别人,学会了品鉴自己,学会了如何集思广益凝聚成一个锐意进取的集体.继单人单词查询 ...

  6. Android Log介绍

    android.util.Log常用的方法有以下5个:Log.v() ,Log.d() ,Log.i() ,Log.w() ,Log.e() .按照日志级别从高到低为ERROR, WARN, INFO ...

  7. 使用 IntraWeb (40) - 自定义 Session 数据

    修改 UserSessionUnit 单元: unit UserSessionUnit; interface uses IWUserSessionBase, SysUtils, Classes, IW ...

  8. oracle 中文乱码---查看和修改客户端字符集

    客户端NLS_LANG的设置方法 Windows: # 常用中文字符集set NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK # 常用unicode字符集 set ...

  9. 专题:Channel Bonding/bonding

    EtherChannel最初是由cisco提出,通过聚合多条物理链路为单条逻辑链路,从而实现高可用及提高吞吐量等目的.AgP(Port Aggregation Protocol,Cisco专有协议). ...

  10. ClickOnce的部署(.appref-ms)在软件限制策略中的解决方案

    为了防止百度.360以及一些小厂商在妈妈的电脑里乱安装各种程序,在域中开启了软件限制策略. 今天在用Github的Windows客户端时发现由于软件限制策略无法运行. Github在Windows中采 ...