LeetCode OJ Count Primes

Description:

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

click to show more hints.

Credits:

Special thanks to @mithmatt for adding this problem and creating all test cases.

素数筛选法。

int countPrimes(int n) {
	bool * IsPrime = (bool*)malloc(sizeof(bool) * n);
	for (int i = 0; i < n; i++) IsPrime[i] = true;
	for (int i = 2; i < n; i++) {
		if (IsPrime[i]) {
			for (int j = i + i; j < n; j += i) {
				IsPrime[j] = false;
			}
		}
	}
	int ans = 0;
	for (int i = 2; i < n; i++) if (IsPrime[i]) ans++;
	free(IsPrime);
	return ans;
}
时间: 2024-10-15 03:43:53

LeetCode OJ Count Primes的相关文章

LeetCode:Count Primes - 统计质数数量

1.题目名称 Count Primes(统计质数数量) 2.题目地址 https://leetcode.com/problems/count-primes/ 3.题目内容 英文:Count the number of prime numbers less than a non-negative number, n. 中文:统计正整数n以内(不含n本身)质数的数量 4.一个TLE的方法 从1到n,考察每个数字是否为质数.这个方法由于花费时间较长,不能满足题目中对时间的要求. 一段实现此方法的Jav

Leetcode problem-204 Count Primes 题解

Leetcode problem-204 Count Primes Count the number of prime numbers less than a non-negative number, n. 题解:这道题如果对每个小于n的数都进行判断是否为素数并计数会超时,因此采用筛法来解这题.建一个数组,从2开始, 把其倍数小于N的都删掉. class Solution { public: int countPrimes(int n) { vector<int>arr(n,1); int s

LeetCode (36) Count Primes

题目描述 Count the number of prime numbers less than a non-negative number, n. 本题要求我们求出小于n的数中共有多少个质数.相信大部分同学在刚开始学习C语言的时候估计都写过判断一个数为质数的程序.一般的思路为: bool isPrime(int num) { int s = sqrt(num) + 1; for( int i = 3; i != s; ++i) { if (num mode i == 0) return fal

[LeetCode 204] Count Primes

题目链接:count-primes Description: Count the number of prime numbers less than a non-negative number, n public class Solution { public int countPrimes(int n) { if(n <= 2) return 0; List<Integer> primes = new ArrayList<Integer>(); primes.add(2);

Java [Leetcode 204]Count Primes

题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime comp

[LeetCode]54. Count Primes统计素数

Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special thanks to @mithmatt for adding this problem and creating all test cases. 解法1:扫描一遍,依次判断每个数是否是素数,会超时Time Limit Exceeded class Solution { public: int coun

[LeetCode][JavaScript]Count Primes

Count Prime Description: Count the number of prime numbers less than a non-negative number, n. https://leetcode.com/problems/count-primes/ 找出所有小于n的数中的质数. 删数法.开一个1到n的数组,删除所有2的倍数,3的倍数...直到√n的倍数,最后剩下的就是质数. 1 /** 2 * @param {number} n 3 * @return {number

LeetCode 204 Count Primes(质数计数)(*)

翻译 计算小于一个非负整数n的质数的个数. 原文 Count the number of prime numbers less than a non-negative number, n. 分析 这道题以前遇到过,当时是用的最笨的办法,现在也没什么好想法,又恰好题目有提示,我就点开了.题目的提示是一条一条给出来的,我也就逐个的全点开了,感觉好失败-- public int countPrimes(int n) { int count = 0; for (int i = 1; i < n; i++

leetcode:Count Primes

Description:Count the number of prime numbers less than a non-negative number, n. 本题给定一个非负数n,让我们求小于n的质数的个数,解题方法就在第二个提示埃拉托斯特尼筛法Sieve of Eratosthenes中,这个算法的过程如下图所示,我们从2开始遍历到根号n,先找到第一个质数2,然后将其所有的倍数全部标记出来,然后到下一个质数3,标记其所有倍数,一次类推,直到根号n,此时数组中未被标记的数字就是质数.我们需