Leetcode#204Count Primes

public class Solution {

public int countPrimes(int n) {

int c=0;

if(n<=1)

return c;

else{

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

{

int v=0;

for(int i=2;i<=Math.sqrt(n);i++)

{

if(j%i==0)

{

v=1;

break;

}

}

if(v==0)

c++;

}

return c;

}

}

}

提交算法后显示

Status:

Time Limit Exceeded


Submitted: 0 minutes ago

Last executed input: 1500000

修改

1. 计算小于n的质数个数,除了2之外,所有的偶数都不是质数,因此只判断所有的奇数

2. 对于某个奇数k,判断其是否为质数,只考虑k是否被2~sqrt(k)中的值整除 

3. 因为本身是奇数,因此只判断是否被2~sqrt(k)中的奇数整除

public class Solution {

public int countPrimes(int n) {

int c=0;

if(n<=2)

return c;

if(n==3)

return ++c;

else{

c++;

for(int j=3;j<n;j=j+2)

{

int v=0;

for(int i=3;i<=Math.sqrt(j);i=i+2)

{

if(j%i==0)

{

v=1;

break;

}

}

if(v==0)

c++;

}

return c;

}

}

}

时间: 2024-08-28 03:55:42

Leetcode#204Count 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-面试算法经典-Java实现】【204-Count Primes(统计质数)】

[204-Count Primes(统计质数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Description: Count the number of prime numbers less than a non-negative number, n. 题目大意 统计小于非负整数n的素数的个数. 解题思路 使用见埃拉托色尼筛法. 代码实现 算法实现类 public class So

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