[LeetCode] 204. Count Primes 解题思路
Count the number of prime numbers less than a non-negative number, n.
问题:找出所有小于 n 的素数。
题目很简洁,但是算法实现的优化层次有很多层。其中最主要思想的是采用 Sieve of Eratosthenes 算法来解答。
大思路为:
- 找出 n 范围内所有合数,并做标记。
- 未做标记的即为素数,统计未做标记的数个数即为原题目解。
如何找到 n 范围内所有合数?
将第一个素数 2 赋值给 i。
当 i 小于 n 时:(2)
- 对于以确定的素数 i ,将 i 的全部倍数标记为合数。(1)
- 离 i 最近的下一个未被标记为合数的数即为素数。将下一个素数赋值给 i .
上面算法有可以优化的地方:
(1)步骤找合数,无需从 2 开始算 i 的倍数,而是从 i 倍开始算,即 i*i。举个例子,当 i 为 5 时, 5*2, 5*3, 5*4 的记号,已经在 i 分别为 2,3,4的时候做了。所以,可以直接从 i 倍开始算。相应地,(2)步骤也可以优化 “为 i*i < n 时”。
int countPrimes(int n) {
vector<bool> res(n, true);
for(long i = ; i * i < n ; i++){
if(res[i] == false){
continue;
}
long square = i*i;
for(long k = ; k * i + square < n ; k++){
res[k * i + square] = false;
}
}
int cnt = ;
for(int i = ; i < n ; i++){
cnt += res[i];
}
return cnt;
}
[LeetCode] 204. Count Primes 解题思路的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- Java for LeetCode 204 Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...
- 【LeetCode】 204. Count Primes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
- [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 click to show more ...
- 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的 ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
随机推荐
- Android深入浅出之 AudioTrack分析
Android深入浅出之Audio 第一部分 AudioTrack分析 一 目的 本文的目的是通过从Audio系统来分析Android的代码,包括Android自定义的那套机制和一些常见类的使用,比如 ...
- Linq101-Element
using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Element ...
- asp.net使用dorpdownlist绑定无限级分类
/// <summary> /// 绑定父项分类 /// </summary> protected void DDLBindClass( ...
- gephi安装好了,为何打不开?
ref: http://www.zhihu.com/question/21268129?sort=created 这个软件我自己也没有弄过,不过我同学要不会装,所以我测试地帮她装,得益于这个哥们的发的 ...
- iOS中使用UIWebView与JS进行交互
iOS中使用UIWebView与JS进行交互 前一段忙着面试和复习,这两天终于考完试了,下学期的实习也有了着落,把最近学的东西更新一下,首先是使用UIWebView与JS进行交互 在webView中我 ...
- Swift - 28 - 内部参数名和外部参数名
//: Playground - noun: a place where people can play import UIKit // 外部参数的作用是为了让程序员调用代码的时候能清晰的看出所传参数 ...
- Swift - 03 - 整数类型
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- java_reflect_04
反射操作数组: 通过public Class<?> getComponentType()来取得一个数组的Class对象 例: import java.lang.reflect.Array ...
- python下 help()使用方法
查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules: help("modules ...
- Cocos2dx 3.2 节点之间相互通信与设置触摸吞噬的方法
实际开发中,我们经常会遇到这样的情况.我们有一个层layer1,这个层包含一个menu层,menu1层里又包含了一个节点按钮button1.现在需要实现一个效果:点击button1弹出一个对话框,这个 ...