本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  2. 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 ...

  3. leetcode-Count Primes 以及python的小特性

    题目大家都非常熟悉,求小于n的所有素数的个数. 自己写的python 代码老是通不过时间门槛,无奈去看了看大家写的code.下面是我看到的投票最高的code: class Solution: # @p ...

  4. Count Primes - LeetCode

    examination questions Description: Count the number of prime numbers less than a non-negative number ...

  5. [leetcode] Count Primes

    Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...

  6. Count Primes

    Count the number of prime numbers less than a non-negative number, n public int countPrimes(int n) { ...

  7. 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 ...

  8. HDU 4715:Difference Between Primes

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  9. hdu 4715 Difference Between Primes

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...

随机推荐

  1. ERROR: In <declare-styleable> MenuView, unable to find attribute android:preserveIconSpacing

    eclipse  sdk从低版本切换到高版本sdk的时候   v7包会包这个错ERROR: In <declare-styleable> MenuView, unable to find ...

  2. EBS业务学习之采购管理

    一.基础数据 w供应商档案 w采购员设置 w审批层次 w单据控制 w危险类代码 w检验代码 w自动来源 w供应商项目w目录册 二.业务流程 w请购单 w询价单 w报价单 w采购定单 w接收 w检验 w ...

  3. cassandra 如何写数据以及放置副本

    application发送数据到server application 发送请求到server 根据设置的load balance 规则从cluster中挑选一个coordinator,一般使用轮询即可 ...

  4. Ubuntu和Windows设备共享

    http://blog.csdn.net/pipisorry/article/details/51725942 蓝牙设备如键盘.鼠标都可以. 装的双系统win7和Ubuntu,如果只使用一个系统,蓝牙 ...

  5. android 获取SD卡的图片及其路径

    1.首先是intent的设置: private static final int IMAGECODE = 0; Intent imageIntent = new Intent(Intent.ACYIO ...

  6. 【移动开发】自定义ProgressBar

    <ProgressBar android:layout_centerInParent="true" android:layout_width="30dp" ...

  7. Cocos2D瓦块地图高清屏(retina)显示比例问题的解决

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在Cocos2D的游戏编程里,常用到瓦块地图.而cocos2D ...

  8. oracle对大对象类型操作:blob,clob,nclob

     1.基本介绍 Oracle和plsql都支持lob(large object) 类型,用来存储大数量数据,如图像文件,声音文件等.Oracle 9i realse2支持存储最大为4g的数据,or ...

  9. Cocos2D iOS之旅:如何写一个敲地鼠游戏(九):创建动画

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  10. 卸载linux后出现grub rescue怎么办?

    原文转自:http://zhidao.baidu.com/link?url=9e2mOttgV0IJDMml58SFbV-7XOvVzp2jR5l1n3ltFOzX1XAcp5-t-QQPc-Nozy ...