Leetcode_204_Count Primes
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46366207
Description:
Count the number of prime numbers less than a non-negative number, n.
思路:
(1)题意为给定整数n,求解n以内的整数中有多少个素数(质数)。
(2)该题涉及到数学相关的知识。首先,看下素数的定义:质数(prime number)又称素数,有无限个。一个大于1的自然数,除了1和它本身外,不能被其他自然数(质数)整除,换句话说就是该数除了1和它本身以外不再有其他的因数;否则称为合数(来自百度百科);其次,对于100以内的数字,发现一下规律:不被2、3、5整数的数是质数,但是超过100就不成立了,例如像11*11=121这样的数,就需要对其是否能开方进行判定;最后,这里通过排除法进行操作,将n以内大于2的数全部存入布尔数组,并将其布尔值值为true,然后进行遍历,在遍历过程中,如果遍历的数值为false,则continue;否则,则从当前下标开始,对当前下标值的平方是否在n范围内进行判定,如果在则将当前下标值置为false,直到遍历结束,最后所得数值中值为true的个数即为素数的个数。
(3)详情见下方代码。希望本文对你有所帮助。该代码为Lettcode官网上的代码,非本人所写。但有必要学习下。
算法代码实现如下:
/**
*
* @author liqqc
*
*/
public class Count_Primes {
public int countPrimes(int n) {
boolean[] isPrime = new boolean[n];
for (int i = 2; i < n; i++) {
isPrime[i] = true;
}
// Loop's ending condition is i * i < n instead of i < sqrt(n)
// to avoid repeatedly calling an expensive function sqrt().
for (int i = 2; i * i < n; i++) {
if (!isPrime[i])
continue;
for (int j = i * i; j < n; j += i) {
isPrime[j] = false;
}
}
int count = 0;
for (int i = 2; i < n; i++) {
if (isPrime[i]){
count++;
}
}
return count;
}
}
Leetcode_204_Count Primes的更多相关文章
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- projecteuler Summation of primes
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two milli ...
- leetcode-Count Primes 以及python的小特性
题目大家都非常熟悉,求小于n的所有素数的个数. 自己写的python 代码老是通不过时间门槛,无奈去看了看大家写的code.下面是我看到的投票最高的code: class Solution: # @p ...
- Count Primes - LeetCode
examination questions Description: Count the number of prime numbers less than a non-negative number ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
- Count Primes
Count the number of prime numbers less than a non-negative number, n public int countPrimes(int n) { ...
- hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...
- HDU 4715:Difference Between Primes
Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- hdu 4715 Difference Between Primes
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...
随机推荐
- 关于bootstrap在IE8下不能支持自适应的问题
说到这个问题,我就想吐槽下IE了,开发这么多版本,每个版本都有一些这样那样的问题不支持,别的正常的浏览器咋都能支持呢?真是垃圾浏览器!!!! 说归说,但是IE现在用的人多啊,怎么办?这个问题还是得解决 ...
- cassandra eclipse 环境构建
摘要 本文主要介绍如何在eclipse中搭建cassandra环境 更多cassandra,nosql 相关知识请访问http://www.webpersonaldeveloper.cn 正文 1.f ...
- java类加载器——ClassLoader
Java的设计初衷是主要面向嵌入式领域,对于自定义的一些类,考虑使用依需求加载原则,即在程序使用到时才加载类,节省内存消耗,这时即可通过类加载器来动态加载. 如果你平时只是做web开发,那应该很少会跟 ...
- ROS_Kinetic_x 目前已更新的常用機器人資料 rosbridge agvs pioneer_teleop nao TurtleBot
Running Rosbridge Description: This tutorial shows you how to launch a rosbridge server and talk to ...
- ubuntu和mac OS X下另一种使用QQ的方法
在ubuntu可以到pidgin官网下载http://www.pidgin.im,然后再安装插件 pidgin-lwqq即可,步骤为: sudo add-apt-repository ppa:lain ...
- DFS(深度优先)算法编程实践
DFS定义 DFS(Depth-First-Search)深度优先搜索算法,是搜索算法的一种.是一种在开发爬虫早期使用较多的方法.它的目的是要达到被搜索结构的叶结点 . 特点 每次深度优先搜索的结果必 ...
- Java基础---Java---IO流-----读取键盘录入、InputStreamReader、转换流、OutputStreamWriter、InputStreamReader
字符流: FileReader FileWriter BufferedReader BufferedWriter 字节流: FileInputStream FileOutputStream Buffe ...
- Java进阶(三十二) HttpClient使用详解
Java进阶(三十二) HttpClient使用详解 Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们 ...
- Android读取/dev/graphics/fb0 屏幕截图
Android屏幕截图有很多方式这里只使用其中一种截图 主要是读取/dev/graphics/fb0,进行转换,复杂点就在如何把读取的数据进行转换. 可以参考一下这篇文章:http://blog.ch ...
- Unity2D Sprite灰白图(Unity3D开发之十一)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=596 昨晚看到群里问到2DSpr ...