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 sum=0;

for(int i=2;i<=n;i++)

{

if(arr[i]==1)

{

sum++;

for(int j=i;j<n;j+=i)

{

arr[j]=0;

}

}

}

return sum;

}

};

时间: 2024-08-05 15:16:41

Leetcode problem-204 Count Primes 题解的相关文章

【leetcode】204 - Count Primes

Description:Count the number of prime numbers less than a non-negative number, n. Hint: 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 complexity

【LeetCode】204. Count Primes 解题小结

题目: Description: Count the number of prime numbers less than a non-negative number, n. 这个题目有提示,计算素数的方法应该不用多说. class Solution { public: int countPrimes(int n) { vector<bool> isPrime; for (int i = 0; i < n; ++i){ isPrime.push_back(true); } for (int

[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 204. Count Primes(线性筛素数)

Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题. class Solution { public: int countPrimes(int n) { int ans=0; vector<int>is_prime(n+1,1); for(int i=2;i<n;i++){ if(is_prime[i]){ ans++; for(int j=2*

(easy)LeetCode 204.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的自然数,该自然数能被1和它本身整除,那么该自然数称为素数. 方法一:暴力破解,时间复杂度为O(N^2) 代码如下: public class

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

204. Count Primes(LeetCode)

Description: Count the number of prime numbers less than a non-negative number, n. 1 class Solution { 2 public: 3 int countPrimes(int n) { 4 vector<bool> num(n - 1, true); 5 num[0] = false; 6 int res = 0, limit = sqrt(n); 7 for (int i = 2; i <= l

leetcode 204. Count Primes 找出素数的个数 ---------- java

Description: Count the number of prime numbers less than a non-negative number, n. 找出小于n的素数个数. 1.用最淳朴的算法果然超时了. public class Solution { public int countPrimes(int n) { if (n < 2){ return 0; } int result = 0; for (int i = 2; i < n; i++){ if (isPrimes(