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 ...
随机推荐
- oracle的回收站介绍
昨天做的展示oracle表空间功能剩余空间的功能,发现查询表dba_free_space时特别慢,经网上搜索,说是由于表空间碎片和回收站(Oracle 10g以后才有)引起的,后来搜到一片介绍回收站的 ...
- 对JavaScript中异步同步机制以及线程深入了解
今天在网上看到各种对Js异步同步单线程多线程的讨论 经过前辈们的洗礼 加上鄙人小小的理解 就来纸上谈兵一下吧~ Js本身就是单线程的 至于为什么Js是单线程的 那就要追溯到Js的历史了 总而言之 由于 ...
- 简单的后台json,前台解析 操作
后台: List<PageData> KeyWords=plantDefDetailCSAService.findKeyWords(pd); JSONArray array = new J ...
- iOS开发-删除字典中的null
删除字典中的null 我们在处理数据库接口的过程中,如果数据中出现null,我们是没法处理的.我在使用NSUserDaults保存后,出现崩溃. null产生原因 null是后台在处理数据的时候,如果 ...
- java分享第七天-02(读取文件)
一 读取文件 public static void main(String[] args) throws FileNotFoundException, IOException { // 建立File对 ...
- c# 反射事件
被反射类中: public delegate void CompeletedHandler(); public static event CompeletedHandler AnalysisCompe ...
- 局域网内利用gitlab,jenkins自动生成gitbook并发布(nginx)
安装了GitBook,内网使用,没法用上gitbook的网页. 用gitbook serve只能展示一本书,而且也不利于长期维护. 于是使用gitlab,jenkins,和nginx配合gitbook ...
- 复制文件的问题:使用FileInputStream和FileOutputStream实现文件复制
public class Test{ public static void main(String [] args) { Test t=new Test(); t.upload(); } public ...
- 7.echo(),print(),print_r()的区别
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print() 只能打印出简单类型变量的值(如int,string) print_r() ...
- FastJson和AsyncHttpCLient
Android的展示数据,除了上章所讲的本地存储外,大部分数据都来自于网络.首先介绍一下Android APP开发常见的网络操作方式.从网络层面上有底层的tcp/ip,也就是我们常见的socket套接 ...