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

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

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

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

  1. class Solution {
  2. public:
  3. int countPrimes(int n) {
  4. bool *isp= new bool[n];
  5. for (int i = ; i < n; ++i)
  6. {
  7. isp[i]= true;
  8. }
  9. for (int i = ; i * i< n; ++i)//素数筛法
  10. {
  11. if(isp[i]){
  12. for(int j = i + i; j < n; j+=i){
  13. isp[j] = false;
  14. }
  15. }
  16. }
  17. int ans = ;
  18. for (int i = ; i < n; ++i){
  19. ans += isp[i];
  20. }
  21. delete isp;
  22. return ans;
  23. }
  24. };

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. 很实用的jQuery事件 - toggle() 方法

    实例 切换不同的背景色: $("p").toggle( function(){ $("body").css("background-color&quo ...

  2. 大道至简之编程的精义读后感(Java伪代码)

    import.java.大道至简.*; import.java.愚公移山.*; public class YuGongYiShan { 愚公={项目组织者,团队经理,编程人员,技术分析师}: //沟通 ...

  3. Android开发涉及有点概念&相关知识点(待写)

    前言,承接之前的 IOS开发涉及有点概念&相关知识点,这次归纳的是Android开发相关,好废话不说了.. 先声明下,Android开发涉及概念比IOS杂很多,可能有很多都题不到的.. 首先由 ...

  4. JSON和js对象之间的相互转化

     jQuery插件支持的转换方式 $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 http://www. ...

  5. Mispelling 1510

    #include<iostream>#include<string>#include<cstdio>using namespace std;int main(){  ...

  6. noip2015-day1-t2

    题意:有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学.游戏开始时,每人都只知道自己的生日.之后每一轮中, ...

  7. [SmartFoxServer入门]服务器安装

    安装SFS2X: SFS2X平台安装操作和步骤都很简单.我们建议先查看对系统的要求,然后根据你选择的操作系统按照指定的安装向导进行安装. 系统要求: SFS2X是一款支持所有主流操作系统,运行在JVM ...

  8. canvas放射性渐变填充

    今天在学习canvas时,遇到canvas的fillstyle有一个createRadialGradient()方法,创建放射性渐变. 上代码: <!DOCTYPE html> <h ...

  9. UART的CTS与RTS

    在RS232中本来CTS 与RTS 有明确的意义,但自从贺氏(HAYES) 推出了聪明猫(SmartModem)后就有点混淆了.在RS232中RTS 与CTS 是用来半双工模式下的方向切换:HAYES ...

  10. [Leetcode][JAVA] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...