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 ... 
随机推荐
- JavaScript入门(7)
			一.什么是函数 函数:把完成特定功能的代码放到一个函数里,直接调用这个函数,就省去重复输入大量代码的麻烦 函数的作用:写一次代码,然后反复地重用这个代码 Eg: 求多组数的和,不使用函数 { var ... 
- 通过修改ajaxFileUpload.js实现多图片动态上传并实现预览
			参考:http://smotive.iteye.com/blog/1903606 大部分我也是根据他的方法修改的,我也要根据name实现动态的多文件上传功能,但是有个问题使我一直无法实现多文件上传. ... 
- Google Maps API 调用实例
			本实例介绍如何调用Google Maps API,并实现用鼠标标注地图,保存进数据库,以及二次加载显示等. 1.需要新建一个自定义控件(如:Map.ascx),用于显示Google地图: <%@ ... 
- ORACLE 11g R2数据库安装硬件环境要求
			物理内存要求:最小1G,在windows7,windows8,windows8.1上最小2G. 虚拟内存(或分页空间)容量要求: Available RAM Swap Space Required B ... 
- 使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC)
			使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC) 程序代码下载 程序代码下载:点此下载 前言 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.N ... 
- UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)
			CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bore ... 
- freemaker小练习
			public class TestFreemaker extends HttpServlet{ // 负责管理FreeMarker模板的Configuration实例 private ... 
- [python] virtualenv下解决matplotlib中文乱码
			1. 安装中文字体 一般系统自带wqy-microhei,其ttc文件位于/usr/share/fonts/truetype/wqy/wqy-microhei.ttc 2. 将ttc文件复制到pyth ... 
- PHP curl 采集内容之规则 1
			<?phpheader("Content-type:text/html; charset=utf-8");$pattern = '/xxx(.*)yyyy/isU'; //i ... 
- php随机生成福彩双色球号码
			发布:thebaby 来源:net [大 中 小] 不瞒您说,俺也是个双色球爱好者,经常买,但迟迟没有中过一等奖,哈哈.这里为大家介绍用php随机生成福彩双色球号码的二种方法,供朋友们学习 ... 
