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 ...
随机推荐
- SQL Server数据的导入导出
SQL Server 2008的导入导出服务可以实现不同类型的数据库系统的数据转换.为了让用户可以更直观的使用导入导出服务,微软提供了导入导出向导.导入和导出向导提供了一种从源向目标复制数据的最简便的 ...
- C#微信公众号开发 -- (六)自定义菜单事件之CLICK
微信公众号中当用户手动点击了按钮,微信公众号会被动的向用户发送文字消息或者图文消息. 通过C#微信公众号开发 -- (五)自定义菜单创建 我们知道了如何将CLICK类型的按钮添加到自己的微信公众平台上 ...
- C10K问题2
http://blog.csdn.net/zhoudaxia/article/details/12920993 是时候让 Web 服务器同时处理一万客户端了,你不觉得吗?毕竟,现在的 Web 是一个大 ...
- Flexbox盒子弹性布局
Can I Use? 2. 概念: 当你给一个元素使用了flexbox模块,那么它的子元素就会指定的方向在水平或者纵向方向排列.这些子元素会按照一定的比例进行扩展或收缩来填补容器的可用空间. < ...
- 暑假集训(2)第二弹 ----- The Suspects(POJ1611)
B - The Suspects Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:20000KB ...
- IniParse解析类
说明 iniParse这个类是一个解析ini文件的类,他的功能和Windows下GetPrivateProfileString的功能一样,可以很方便的保存读取配置. 当然他不是只有GetPrivate ...
- Iptables網路連線限制及攻擊防護和相關設定
[筆記整理]Iptables網路連線限制及攻擊防護和相關設定 1. 限制每個IP連接HTTP最大併發50個連接數 iptables -A INPUT -p tcp --dport 80 -m conn ...
- docker中搭建gitlab
1, 下载镜像 docker pull sameersbn/gitlab:7.4.3 # 下载gitlab镜像 docker pull sameersbn/mysql:latest # 下载gitla ...
- Android学习5—布局简介
Android界面的布局主要有四种,分别为RelativeLayout.LinearLayout.TableLayout.FrameLayout,接下来分别介绍这些布局如何使用(为了简单起见,接下来的 ...
- 利用iptables来配置linux禁止所有端口登陆和开放指定端口
from:http://www.myhack58.com/Article/sort099/sort0102/2011/31781.htm 1.关闭所有的 INPUT FORWARD OUTPUT 只对 ...