Leetcode(204) Count Primes
题目
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;
}
};
Leetcode(204) Count Primes的更多相关文章
- Leetcode(59)-Count Primes
题目: Description: Count the number of prime numbers less than a non-negative number, n. 思路: 题意:求小于给定非 ...
- LeetCode(38) Count and Say
题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111 ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
随机推荐
- 这个匿名对象没有实现IComparable接口
https://www.cnblogs.com/felixnet/p/5193086.html https://docs.microsoft.com/zh-cn/dotnet/api/system.i ...
- Kaggle八门神器(一):竞赛神器之XGBoost介绍
Xgboost为一个十分有效的机器学习模型,在各种竞赛中均可以看到它的身影,同时Xgboost在工业届也有着广泛的应用,本文以Titanic数据集为研究对象,简单地探究Xgboost模型建模过程,同时 ...
- MoinMoin install in apache (win)
一:下载环境 xampp:http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.8.1/xampp-win32-1.8.1-VC9 ...
- 定时器 & 日期时间对象 & 正则
1 JavaScript 计时事件 通过使用 JavaScript,有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行,这称之为计时事件. 两个关键方法是: setInterv ...
- VC中包含的头文件名不区分大小写
VC中包含的头文件名,不区分大小写如 #include "my.h" = #include "MY.H".
- android 跨进程通讯 AIDL
跨进程如何通讯?两个进程无法直接通讯,通过Android系统底层间接通讯.基于service的aidl实现跨进程通讯. 什么叫AIDL? Android interface definition la ...
- 如何变更站点 AD 域服务器IP地址
在 winserver 2012 单森林单域,多站点环境中,想把某一个站点AD 域服务器IP地址更改,要如何操作,才能保证客户端正常运行,不影响客户端的运行.有些朋友也经常提出类似问题. 想在不影响 ...
- mysql-单表操作
mySql单表操作主要有一下内容: 1.查询:查询又分为几种,范围查询,模糊查询.空值查询.多条件查询 查询的语句格式为:SELECT 字段 1,字段 2,字段 3...FROM 表名 WHERE 条 ...
- 【Python图像特征的音乐序列生成】第一阶段的任务分配
从即日起到7月20号,项目成员进行了第一次任务分配. 赵同学A.岳同学.周同学,负责了图像数据的情感数据集制作,他们根据自己的经验,对图像进行了情绪提取. 赵同学B全权负责向量映射这一块的网络搭建. ...
- Installing Apache, PHP, and MySQL on Mac OS X
I have installed Apache, PHP, and MySQL on Mac OS X since Leopard. Each time doing so by hand. Each ...