leetcode 204
题目描述:
Description:
Count the number of prime numbers less than a non-negative number, n.
解法一:
遍历从1-n的所有整数,查看是否为质数,是质数借助一个则将该整数存入一个容器中,判断一个数是否为质数,可以遍历在容器中且小于n的平方根的质数,如果n可以被符合条件的质数整除,则这个数不是质数。代码如下:
class Solution {
public:
vector<int> prime_vec;
bool isPrime(int n)
{
if (n<)
return false;
else if (n == )
return true;
else if (n % == )
return false;
else
{
int n_sqr = sqrt(n);
for (int i = ; prime_vec[i] <= n_sqr; i ++) {
if(n % prime_vec[i] == )
return false;
}
return true;
}
}
int countPrimes(int n) {
int counter = ;
for (int i = ; i < n; i ++) {
if (isPrime(i)) {
prime_vec.push_back(i);
counter ++;
}
}
for (auto i = prime_vec.begin(); i != prime_vec.end(); i ++) {
cout << *i << endl;
}
return counter;
}
};
解法二:
上述代码的执行效率不高,看了其他人的解题思路之后,豁然开朗,维基百科上有一个动态演示的效果图,算法思想名叫“晒数法”,连接如下:
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
看了这个效果图我马上进行了自己的实现,代码如下:
class Solution {
public:
int countPrimes(int n) {
int counter = ;
if (n < )
return counter;
int upper = sqrt(n);
vector<bool> flag_vec(n, false);
int i = ;
while (i < n) {
cout << i << endl;
counter ++;
if(i <= upper)
{
for (long long j = i * i; j < n; j += i)
flag_vec[j] = true;
}
++ i;
while (flag_vec[i] == true)
++ i;
}
return counter;
}
};
可以很清楚地知道,解法二比解法一要好很多,因为在从小到大遍历的过程中,所有的数仅遍历一遍,这解法一虽然比暴力的O(n*n)的方法好一些,但是时间复杂度还是大于O(n)的,而晒数法的时间复杂度仅为O(n)。
leetcode 204的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- Leetcode 204计数质数
计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 比计算少n中素数的个数. 素数又称质 ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Java实现 LeetCode 204 计数质数
204. 计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . class Solutio ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- Java for LeetCode 204 Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...
- Leetcode 204 Count Primes 数论
题意:统计小于n的质数个数. 作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法. 是的,素数筛法并不是该题最佳的解法,线性素数筛法才是. 至于什么是素数筛法,请百度吧. class Sol ...
随机推荐
- ios数据永久存储之----NSUserDefaults
我们在开发app时不可避免的会在本地存储一些数据,NSUserDefaults就是系统提供的一个用来数据存储的类,本片文章就来介绍一些NSserdefazults的用法. 详细内容:https://m ...
- Oracle 第一天
Oracle 第一天 1.oracle数据库下载.安装和配置 1.1 下载压缩包后解压并将压缩包2里面的文件覆盖至压缩包1中 1.2 按照步骤逐步安装 1.3 设置管理员密码时,默认情况下四个管理员是 ...
- html基本知识点
<hr />定义下划线 <br/>定义换行<p>段落</p> <h1>标题</h1>h后的数字逐渐增大,字体逐渐减小;<h ...
- BurpSuite设置公共WIFI抓包
1.电脑连接公共WIFI
- 解决SVN更新代码是出现previous operation has not finished; run cleanup if it was interrupted这个错误
解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.db (查看.svn文件需要打开显示隐藏文件夹) 3.将sqlite3.exe放到.sv ...
- map 和 vector 的erase函数说明
1. map的erase函数使用 这里首先要注意,C++针对map的erase函数有不同的函数原型,这往往是出现问题的关键所在.根据参考文献1: 在C++98中: (1) void erase (it ...
- htaccess分布式配置文件常用写法
htaccess 写法 Apache中的.htaccess(或者”分布式配置”了针对目录改变配置的方法,即,在特定的文档目录中放置包含或多个指令的,以作用于此目录及其子目录.作为,所能的命令受到限制. ...
- lua52 C API测试代码
//这是一篇lua与C++交互的情景测试 #include <lua.hpp> #include <lauxlib.h> #include <lualib.h> # ...
- Linux CentOS6.5下安装Oracle ASM
Oracle版本:Oracle 11g 1.确定自己的Linux版本: [root@localhost ~]#uname -r 2.6.32-431.el6.x86_64 2.6.32-431.el6 ...
- Jquery事件:鼠标移入移出(mouseenter,mouseleave)
前几天帮朋友做了一个单页面,其中有个效果就是鼠标移动到头像上变换头像样式,当鼠标移出时恢复头像样式.当时没多想,脑子就蹦出了mouseover,mouseout两个方法. 但是在编写页面的过程中,无论 ...