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 ...
随机推荐
- Can't find PHP headers in /usr/include/php
解决办法: yum install php-devel
- cas单点登录时报Invalid credentials
CAS4后默认的密码验证不是简单的相同了.在配置文件deployerConfigContext.xml里可以找到, 默认是 Username:casuser Password:Mellon
- UDP协议开发
UDP是用户数据报协议(User Datagram Protocol,UDP)的简称,其主要作用是将网络数据流量压缩成数据报形式,提供面向事务的简单信息传送服务.与TCP协议不同,UDP协议直接利用I ...
- zorka源码解读之Beanshell与zorka的交互实现
一.beanshell基础知识从应用程序中调用BeanShell创建一个BeanShell的解释器(interpreter)用eval()和source()命令可以对一个字符串求值和运行一个脚本文件使 ...
- 【BZOJ】3521: [Poi2014]Salad Bar
题意 长度为\(n(1 \le n \le 1000000)\)的\(01\)字符串.找一个最长的连续子串\(S\),使得不管是从左往右还是从右往左取,都保证每时每刻已取出的\(1\)的个数不小于\( ...
- HDU 3308 LCIS(线段树)
题目链接 模板题吧,忘了好多,终于A了... #include <cstring> #include <cstdio> #include <string> #inc ...
- Nginx日常操作和配置
安装位置:/usr/local/nginx配置目录:/usr/local/nginx/conf配置文件:/usr/local/nginx/conf/nginx.conf启动命令:/usr/local/ ...
- Http状态码笔记
1,503 服务器不可用. HTTP Error 503错误的解释:web服务器不能处理HTTP请求,可能是临时超载或者是服务器进行维护.这意味着你需要忍耐一下,等待服务器的临时处理.在这种状态下,一 ...
- *HDU1969 二分
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- C#解析HTML
第一种方法:用System.Net.WebClient下载Web Page存到本地文件或者String中,用正则表达式来分析.这个方法可以用在Web Crawler等需要分析很多Web Page的应用 ...