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)的范围内,标记出 非素数,剩下的就是素数了。

思路:

  1. 初始化所有的[2,n)都是素数
  2. 剔除掉 非素数
  3. 统计素数个数

如何标记 非素数呢?分组标记:

  • 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的更多相关文章

  1. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  2. leetcode面试准备: Game of Life

    leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...

  3. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  4. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  5. leetcode面试准备:Reverse Words in a String

    leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...

  6. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  7. leetcode面试准备:Triangle

    leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...

  8. leetcode面试准备:Sliding Window Maximum

    leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...

  9. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

随机推荐

  1. Retrofit研究1

    以下为来自Github的retrofit的介绍.话说,翻译还真蛋疼,就这个花了快3个小时. Retrofit 一个在Android和Java上类型安全的HTTP客户端 介绍 Retrofit可以把你的 ...

  2. ACM——A + B Problem (4)

    A + B Problem (4) 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:2496            测试通过:124 ...

  3. 软件工程——UML简介

    UML概述: UML是对OMT(对象建模技术).Booth(Booch方法)以及OOSE(面向对象的软件工程)等记号系统实施统一工作后得到的一种记号系统. UML(Unified Modeling L ...

  4. IAP (In-App Purchase)中文文档

    内容转自:http://yarin.blog.51cto.com/1130898/549141 一.In App Purchase概览 Store Kit代表App和App Store之间进行通信.程 ...

  5. javax.el.PropertyNotFoundException: Property 'aDesc' not found on type

    这个问题是是在我使用jeesite自动代码是产生的,原因是实体类的属性命名规范不合格,我在网上看到类的属性前三个字母不能出现大写 解决办法:将类的属性大小写改一下

  6. Record:逻辑分区下新建简单卷后其他卷被删除

    上图是恢复后的磁盘情况,恢复前的情况没有截图. 事情是这样:扩展分区中原本有4个逻辑分区.想将其中一个分区(MySpace,第一个分区)压缩出部分空间新建一个分区.经过 压缩卷->新建简单卷 后 ...

  7. NFS,FTP

    一. NFS1. NFS简介NFS全称是network file systemNFS允许一个系统在网络上与他人共享目录和文件.通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件. 假 ...

  8. tr设置border无效的解决方法

    给table的css添加属性:border-collapse: collapse; 边框不折叠的表格 行,列,行组是不具有border的

  9. 尝试使用Java6API读取java代码

    主要类:JavaCompiler  FileManager JavaCompiler .CompilationTaskAbstractProcessor参考代码https://today.java.n ...

  10. 玩转HTML5移动页面(动效篇)

    原文:http://www.grycheng.com/?p=458 作为一名前端,在拿到设计稿时你有两种选择: 1.快速输出静态页面 2.加上高级大气上档次狂拽炫酷屌炸天的动画让页面动起来 作为一个有 ...