作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/count-primes/

Total Accepted: 36655 Total Submissions: 172606 Difficulty: Easy

题目描述

Count the number of prime numbers less than a non-negative number, n.

Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

题目大意

计算小于n的素数有多少个。

解题方法

素数筛法

http://blog.csdn.net/blitzskies/article/details/45442923 提示用Sieve of Eratosthenes的方法。

素数筛法就是把这个数的所有倍数都删除掉,因为这些数一定不是素数。最后统计一下数字剩余的没有被删除的个数就好。

也是学习了。

Java解法。

重点是优化效率,每一步的效率都要优化。

用List都会效率低,最后用数组好了。

/**
* 统计质数的数目。使用数组,用列表效率低。
*
* @param n
* @return
*/
public static int countPrimes3(int n) { //n个元素的数组,其实只用到了n-2个。多见了2个防止输入0的时候崩溃。
int[] nums = new int[n];
//把所有的小于n的大于2的数字加入数组里
for (int i = 2; i < n; i++) {
//注意计算质数从2开始的,而数组从0开始
nums[i - 2] = i;
}
//统一的长度,优化计算
int size = nums.length;
//各种优化效率,只计算到sqrt(n)
for (int i = 0; i * i < size; i++) {
//获取数组中的数字
int temp = nums[i];
//如果不是0,则进行计算,否则直接跳过,因为这个数已经是之前的某数字的倍数
if (temp != 0) {
//把该数字的倍数都删去,因为他们都不是质数
//int i1 = temp - 1 优化效率,是因为比如用5来删除数字的时候15=3*5已经被删除过了,所以从20=5*4开始删除
for (int i1 = temp - 1; i + temp * i1 < size; i1++) {
//如果这个数不是素数则被置0,置成其他的负数也一样,只是为了区分和统计
//i + temp * i1,i是因为从当前数字开始,比如10是从5的位置开始计算位置
nums[i + temp * i1] = 0;
}
}
}
return countNums(nums);
} /**
* 计算数组中的0出现了多少次
*
* @param nums
* @return
*/
public static int countNums(int[] nums) {
int size = nums.length;
int zeros = 0;
for (int num : nums) {
if (num == 0) {
zeros++;
}
} return size - zeros;
}

没必要把所有的数字都保存到一个数组里面,可以直接记录和数字对应的位置的数字是不是质数。如果不是质数,则在对应位置保存true.最后统计不是true的,即质数的个数即可。

这个方法可以通用下去。类似的统计的题目只记录对应的位置是否满足条件,最后统计符合条件的个数。

/**
* 统计质数的数目。使用数组,用列表效率低。
*
* @param n
* @return
*/
public static int countPrimes4(int n) { //n个元素的数组,其实只用到了n-2个。多见了2个防止输入0的时候崩溃。
boolean[] nums = new boolean[n];
//各种优化效率,只计算到sqrt(n)
for (int i = 2; i * i < n; i++) {
//获取数组中的数字是不是为0
//不是质数则为true
boolean temp = nums[i];
//如果不是true,说明不是质数,则进行计算,否则直接跳过,因为这个数已经是之前的某数字的倍数
if (!temp) {
//把该数字的倍数都删去,因为他们都不是质数
//int i1 = temp - 1 优化效率,是因为比如用5来删除数字的时候15=3*5已经被删除过了,所以从20=5*4开始删除
for (int j = i; i * j < n; j++) {
//如果这个数不是素数则被置true
//i + temp * i1,i是因为从当前数字开始,比如10是从5的位置开始计算位置
nums[i * j] = true;
}
}
}
return countNums2(nums);
} /**
* 计算数组中的不是素数的false出现了出现了多少次
*
* @param nums
* @return
*/
public static int countNums2(boolean[] nums) {
int notZeros = 0;
for (int i = 2; i < nums.length; i++) {
if (!nums[i]) {
notZeros++;
}
} return notZeros;
}

二刷,使用Python解法,速度很慢,勉强通过了。

class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
nums = [True] * n
for i in xrange(2, n):
j = 2
while i * j < n:
nums[i * j] = False
j += 1
res = 0
for i in xrange(2, n):
if nums[i]:
res += 1
return res

C++版本如下:

class Solution {
public:
int countPrimes(int n) {
vector<bool> nums(n, true);
for (int i = 2; i < n; i++) {
int j = 2;
while (i * j < n) {
nums[i * j] = false;
j ++;
}
}
int res = 0;
for (int i = 2; i < n; i++) {
if (nums[i]){
res ++;
}
}
return res;
}
};

C++数组初始化需要使用memset,而且数组的大小n不能是0,数组解法如下。

class Solution {
public:
int countPrimes(int n) {
if (n <= 0) return false;
bool nums[n];
memset(nums, true, sizeof(nums));
for (int i = 2; i < n; i++) {
int j = 2;
while (i * j < n) {
nums[i * j] = false;
j ++;
}
}
int res = 0;
for (int i = 2; i < n; i++) {
if (nums[i]){
res ++;
}
}
return res;
}
};

参考资料

http://blog.csdn.net/blitzskies/article/details/45442923

https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#cite_note-horsley-1

http://blog.csdn.net/xudli/article/details/45361471

日期

2015/10/19 23:38:24
2018 年 11 月 29 日 —— 时不我待

【LeetCode】 204. Count Primes 解题报告(Python & C++)的更多相关文章

  1. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

  2. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  3. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  4. [LeetCode] 204. Count Primes 计数质数

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

  5. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  6. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  7. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  8. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  9. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

随机推荐

  1. springboot与数据访问之jdbc

    官网的starthttps://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter 添加依 ...

  2. Bootstrap实战 - 瀑布流布局

    讲 Bootstrap 基础的教程网上已经很多了,实际上 Bootstrap 中文网(bootcss.com)里的文档已经写的很详细了,但实战的案例却不多.这里用一些当前流行的网页布局为导向,使用 B ...

  3. Vector总结及部分底层源码分析

    Vector总结及部分底层源码分析 1. Vector继承的抽象类和实现的接口 Vector类实现的接口 List接口:里面定义了List集合的基本接口,Vector进行了实现 RandomAcces ...

  4. C/C++ Qt 数据库与TreeView组件绑定

    在上一篇博文<C/C++ Qt 数据库QSql增删改查组件应用>介绍了Qt中如何使用SQL操作函数,并实现了对数据库的增删改查等基本功能,从本篇开始将实现数据库与View组件的绑定,通过数 ...

  5. A Child's History of England.8

    CHAPTER 3 ENGLAND UNDER THE GOOD SAXON, ALFRED Alfred [born in 849 CE, 唐: 618年-907年] the Great was a ...

  6. Learning Spark中文版--第三章--RDD编程(1)

       本章介绍了Spark用于数据处理的核心抽象概念,具有弹性的分布式数据集(RDD).一个RDD仅仅是一个分布式的元素集合.在Spark中,所有工作都表示为创建新的RDDs.转换现有的RDD,或者调 ...

  7. Output of C++ Program | Set 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  8. 『与善仁』Appium基础 — 24、等待activity出现

    目录 1.什么是等待activity出现 2.wait_activity()方法 3.获取当前页面的activity方法 4.综合练习 1.什么是等待activity出现 在启动APP的时候,要配置包 ...

  9. &和nohup

    目录 一.简介 二.& 三.nohup 一.简介 当我们在终端或控制台工作时,可能不希望由于运行一个作业而占住了屏幕,因为可能还有更重要的事情要做,比如阅读电子邮件.对于密集访问磁盘的进程,我 ...

  10. Tableau如何绘制瀑布图

    一.将子类别拖至列,利润拖拽至行,类型改为甘特条形图 二 右键利润-快速表计算-汇总(数据会从左向右显示累计汇总) 三.创建计算字段-[利润] 四.将负利润拖拽到大小,利润拖拽到颜色 分析-合计-显示 ...