1. Count Primes

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

Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

解法1 埃氏筛法

class Solution {
public:
int countPrimes(int n) {
if(n < 2)return 0;
vector<bool>isPrime(n, true);
isPrime[0] = false;
isPrime[1] = false;
int ans = 0;
for(int i = 2; i < n; ++i){
if(isPrime[i]){
ans++;
int k = 2;
while(k * i < n){
isPrime[k*i] = false;
k++;
}
}
}
return ans;
}
};

解法2 欧拉筛法(线性筛)

class Solution {
public:
int countPrimes(int n) {
if(n < 3)return 0;
vector<bool>isPrime(n, true);
vector<int>Prime(n);
isPrime[0] = false;
isPrime[1] = false;
int cnt = 0;
for(int i = 2; i <= n -1 ; ++i){
if(isPrime[i]){
Prime[cnt++] = i;
}
for(int j = 0; j < cnt && i * Prime[j] < n; ++j){
isPrime[Prime[j]*i] = false;
}
}
return cnt;
}
};

【刷题-LeetCode】204. Count Primes的更多相关文章

  1. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  2. LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes

    原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...

  3. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  4. [LeetCode] 204. Count Primes 计数质数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  5. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  6. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  7. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  8. leetcode 204. Count Primes(线性筛素数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题. c ...

  9. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  10. Leetcode 204 Count Primes 数论

    题意:统计小于n的质数个数. 作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法. 是的,素数筛法并不是该题最佳的解法,线性素数筛法才是. 至于什么是素数筛法,请百度吧. class Sol ...

随机推荐

  1. word文档打钩记录快捷键

    先按住键盘上的 Alt 键不放,然后在小键盘区(数字键区)输入 9745 ,最后松开 Alt键.

  2. IDEA通过git回滚到某个提交节点或某个版本

    1.项目右键后,点击"Git - Show History" 这里会显示有历史提交的版本记录(这里我们假设要回滚到 "提交" 版本中) 2.选中 "提 ...

  3. JAVA获取当前日期所在的周一到周日的所有日期集合

    /** * 获取当前日期所在的周一到周日的所有日期集合 * @return */ public static List<Date> getWeekDateList() { Calendar ...

  4. 【LeetCode】1413. 逐步求和得到正数的最小值 Minimum Value to Get Positive Step by Step Sum

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 日期 题目地址:https://leetcode ...

  5. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

  7. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  8. Towards Deep Learning Models Resistant to Adversarial Attacks

    目录 概 主要内容 Note Madry A, Makelov A, Schmidt L, et al. Towards Deep Learning Models Resistant to Adver ...

  9. Latex 入门教程

    Latex 入门教程 学习途径:LaTex入门_哔哩哔哩_bilibili 运行环境:texlive2021.texstudio-4.1.2-win-qt6 1. 基本结构 整个 Latex 文件分为 ...

  10. [数据结构]链表相关的实现LinkList.cpp

    目录 LinkList.cpp //链表相关操作的实现 LinkList.h LinkListManager.cpp //链表相关实现函数的调用 LinkListManager.h LinkList. ...