题目

Description:

Count the number of prime numbers less than a non-negative number, n.

Credits:

Special thanks to @mithmatt for adding this problem and creating all test cases.

Hint:

Let’s start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better?

分析

计算小于n的非负整数中素数的个数。

这本是一个简单的题目,直观的反应,就是逐个判断并累计数目,但是直接这样提交的话得到的肯定是TLE的。因为本题的核心便是考察的性能问题。

素数的动态演示,此处给出了素数累计的另一种思路,我们知道任何一个素数的倍数都是合数,从此出发,先初始化一个n个单元的标志数组,将每个元素标记为素数,然后将素数的倍数标志改为false。最后遍历标志数组,得到素数的个数。

下面给出三种实现,方法一是TLE的算法,方法二和方法三核心思想一样,只不过是采用了不同的累计方式,是AC的。

AC代码

class Solution {
public:
//方法一:逐个判断O(n^(3/2))算法,TLE
int countPrimes1(int n) {
int count = 0;
for (int i = 2; i < n; ++i)
{
if (isPrime(i))
++count;
}
return count;
} bool isPrime(int n)
{
for (int i = 2; i <= sqrt(n); ++i)
if (n % i == 0)
return false;
return true;
} //方法二:素数不能被比它小的整数整除,建一个标志数组,从2开始,把其倍数小于N的都删掉
int countPrimes2(int n)
{
vector<int> flags(n + 1, 1);
for (int i = 2; i*i <= n; ++i)
{
//把素数的倍数全部设置为非素数标志
if (flags[i])
{
//内层循环从i开始, 比i小的会在以前就被check过
for (int j = i; i*j < n; ++j)
{
flags[i*j] = 0;
}//for
}//for
}//for int count = 0;
for (int i = 2; i < n; ++i)
{
if (flags[i])
++count;
}
return count;
} //方法三,与二思路相同,另一种实现方式
int countPrimes(int n)
{
if (n <= 2)
return 0; vector<int> flags(n, 1);
//记录素数的个数
int count = n - 2;
for (int i = 2; i*i <= n; ++i)
{
if (flags[i])
{
//内层循环从i开始, 比i小的会在以前就被check过
for (int j = i; i*j < n; ++j)
{
//如果原来标记的为素数,现在改为非素数,同时更新素数个数
if (flags[i*j])
{
flags[i*j] = 0;
--count;
}//if
}//for
}//if
}//for
return count;
}
};

GitHub测试程序源码

Leetcode(204) Count Primes的更多相关文章

  1. Leetcode(59)-Count Primes

    题目: Description: Count the number of prime numbers less than a non-negative number, n. 思路: 题意:求小于给定非 ...

  2. LeetCode(38) Count and Say

    题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111 ...

  3. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  4. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  5. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. stm32f107的使用:

    一 不能支持软件仿真: 二 外部晶体推荐25MHZ,但如果不用音频接口,也可以使用8M晶体,需修改这里成8000000: 此时设置如下: 并修改这里 改为: 因为

  2. openstack修改增加网卡及更改ip

        1.neutron更改及mysql数据库更改网卡及ip:     neutron port-update ae22c84b-22a9-4618-b046-1eb61379bcea  --all ...

  3. 详解Java构造方法为什么不能覆盖,我的钻牛角尖病又犯了....

    一 看Think in Java,遇到个程序 class Egg2 { protected class Yolk { public Yolk() { System.out.println(" ...

  4. Mybatis find_in_set 子查询,替代 in

    1. Mapper文件 2.dao层 3.生成Sql

  5. springboot 学习笔记(三)

    (三)用jar包启动springboot项目 1.首先需要在pom文件中添加依赖,spring-boot-starter-parent包含有打包的默认配置,如果要修改的话要可以进行重新定义,具体内容参 ...

  6. java编程基础二进制

    0.java编程基础 01.二进制(原码,反码,补码) 02.位运算 03.移位运算符 二进制 原码,反码,补码 1.基本概念 二进制是逢2进位的进位制,0,1是基本算符. 现在的电子计算机技术全部使 ...

  7. [整理]Magento2 开发中遇到的错误以及解决方案

    下面记录了一些我在二次开发Magento2是所遇到的错误.错误原因以及解决方案: 1. Object DOMDocument should be created type: report 日志摘要: ...

  8. cf519D. A and B and Interesting Substrings(前缀和)

    题意 给出$26$个字母对应的权值和一个字符串 问满足以下条件的子串有多少 首尾字母相同 中间字母权值相加为0 Sol 我们要找到区间满足$sum[i] - sum[j] = 0$ $sum[i] = ...

  9. Python+selenium之selenium Grid2

    利用selenium grid2 keyi可以在不同的主机上建立主节点(hub)和分支节点(node),可以使主节点上的测试用例在不同的分支节点上运行.对不同的节点来说,可以搭建不同的测试环境(操作系 ...

  10. 新手第一天学习 C#语言(进制互换)

    说起来我们对一些陌生或者未知的东西有一些恐惧感,但是又有一些期待,虽然我不确定自己能不能学会这门语言,但是我会尽自己最大的努力学. 我们第一天学的的内容呢,对大多数的人都知道,计算机的语言是二进制,但 ...