leetcode面试准备: CountPrimes
1 题目
Description:
Count the number of prime numbers less than a non-negative number, n.
接口:public int countPrimes(int n);
2 思路
统计小于n的素数个数,注意不包括n。
思路1:素数的判断
很容易想到素数的判断isPrime,然后逐个数来进行判断是否是素数。进行统计,输出结果。
复杂度: 把isPrime时间复杂度控制在O(n^0.5)的话,因此:Time:O(n^1.5) , Space:O(1)。
提交代码仍然超时。
思路2: 素数表
素数表的产生,在[1 to n)的范围内,标记出 非素数,剩下的就是素数了。
思路:
- 初始化所有的
[2,n)都是素数 - 剔除掉
非素数 - 统计
素数个数
如何标记 非素数呢?分组标记:
- 2,[4,6,8,10,12,14,16,18,20,22,24...];
- 3,[9,12,15,18,21,24,27,...];
- 5,[25,30,35...]
- 考虑一下终止条件
复杂度: Time: O(n log log n) , Space: O(n)
3 代码
思路1
// 判断一个数是否是素数的方法:不是最好,但还可以。素数表是判断素数的好方法。
// Time:O(sqrt(n)) Space:O(1)
boolean isPrime(int n) {
for (int i = 2; (i * i) <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
/**
* Solution 1: 逐个判断是否是素数,思路简单。但是超时
* Time:O(n^1.5) Space:O(1)
*/
public int countPrimes(int n) {
int count = 0;
for (int i = 2; i <= n; i++) {
if(isPrime(i)) count++;
}
return count;
}
思路2
// Time: O(n log log n) Space: O(n)
public int countPrimes(int n) {
// 初始化所有的都是素数,在剔除掉 `非素数`
boolean[] isPrime = new boolean[n];
for (int i = 2; i < n; i++) {
isPrime[i] = true;
}
// 剔除非素数
for (int i = 2; (i * i) < n; i++){
if (isPrime[i]) {
for (int j = i * i; j < n; j += i) {
isPrime[j] = false;
}
}
}
// 统计素数个数
int count = 0;
for (boolean is : isPrime) {
if (is) count++;
}
return count;
}
4 总结
素数是比较经典的题目。此题的tag: HashTable, Math
5 参考
leetcode面试准备: CountPrimes的更多相关文章
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- leetcode面试准备: Game of Life
leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- leetcode面试准备:Reverse Words in a String
leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- leetcode面试准备:Triangle
leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...
- leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...
- leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
随机推荐
- java学习笔记--1_常见输入输出语句熟悉篇章
今天上oj,想来是准备做做算法和数据结构的.看了看以前做的基础题目,想着就先熟悉一下java的语言吧! 以下是今天做的10道题目. 备注:oj有时候对格式要求非常严格,因为不在格式上纠结太久,只要ec ...
- python中关于正则表达式三
2015年8月14日 11:10 7.2正则表达式操作 正则表达式使用反斜杠字符'\'来暗示一些特殊的形式或者允许特殊的字符使用但是没有调用它们特殊的意思.在字符串常量中的相同目标的字符的python ...
- JavaScript 学习笔记-- ES6学习(一)介绍以及Babel的使用
本文摘自阮一峰老师的<ECMAScript 6入门>,原文地址:http://es6.ruanyifeng.com/#docs/intro ECMAScript 6 是一个泛指,含义是5. ...
- Nginx的反向代理
先通过简单的图来说明一下正向代理和反向代理吧~ 正向代理 代理其实就是一个中介,A和B本来可以直连,中间插入一个C,C就是中介.刚开始的时候,代理多数是帮助内网client访问外网server用的(比 ...
- (poj) 1751 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...
- C++ GUI Programming with Qt4 笔记 -- chap1
1. Hello Qt #include <QApplication> #include <QLabel> int main(int argc, char *argv[]){ ...
- 九度OJ 1084 整数拆分
题目地址:http://ac.jobdu.com/problem.php?pid=1084 题目描述: 一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 ...
- C程序的构成及动态内存分配
对一个程序,通常的理解就是,源码编译成机器代码,然后通过机器解释运行.不过是怎样编译成机器代码,和怎样运行的,无疑是个值得探讨的问题.怎样编译成机器代码,过程就是源码的编译.链接,编译器做了这些事.而 ...
- Sublime Text3 个人使用心得
sublime与webstorm的比较: webstorm真心很强大,强大到能够几乎满足所有前端开发者编程的需求,方便的快捷键操作.代码提示.浏览器查看.工程管理.历史记录(可以找到之前编辑的内容,即 ...
- demo_07选择器练习
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...