题意:统计小于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. Scrum会议(Beta版本)

    组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...

  2. [付费视频]Delphi视频Android开发使用静态库(A)和动态库(SO)

    关于本视频:前阵子接到一个委托,解决Delphi开发Android程序中串口通信的问题,厂家那边提供了c文件,需要翻译成delphi可用,翻译倒是比较简单.不过后来翻译读写ic卡单元的时候进行不下去了 ...

  3. VB6.0 获取Excel文件工作表Sheet的名称

    获取Excel文件工作表Sheet的名称 '产生Excel文档 Dim xlapp, xlbook As Object Dim sSheetName As String Set xlapp = Cre ...

  4. vb.net加密解密方法

    1.vb.net加密解密方法 Private Function getLicenseDate() As String Dim b() As Byte Dim path As String = Serv ...

  5. 【java】:解析xml

    ==========================================xml文件<?xml version="1.0" encoding="GB231 ...

  6. hd2066一个人的旅行

    Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰 ...

  7. IntelliJ IDEA 目录技巧

    IntelliJ IDEA的Web应用的目录结构 Ø 目录图: Ø 目录解释: 开发目录 目录名称 描述 Test 工程名称 lib Jar包的存放目录 src 源文件也就是文件(类,资源文件)存放的 ...

  8. Spring的定时任务配置(转)

    spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTas ...

  9. 基于注解的DWR使用

    dwr3.0支持使用注解,如果不喜欢配置dwr.xml文件,注解是个不错的方法,简单快捷. 步骤如下: 1.配置web.xml文件,需要在DwrServlet里加classes初始化参数: <i ...

  10. opengles tutorial

    https://developer.apple.com/library/ios/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide ...