- 浏览: 171720 次
- 性别:
- 来自: 济南
文章分类
最新评论
Description:
Count the number of prime numbers less than a non-negative number, n.
给定一个整数,返回小于它的所有质数。我们借助一个布尔数组,来记录哪些数已经被标记为不为质数,每次循环的时候都从一个质数开始。最后只需要遍历一遍布尔数组中值为true的元素个数就可以了。代码如下:
Count the number of prime numbers less than a non-negative number, n.
给定一个整数,返回小于它的所有质数。我们借助一个布尔数组,来记录哪些数已经被标记为不为质数,每次循环的时候都从一个质数开始。最后只需要遍历一遍布尔数组中值为true的元素个数就可以了。代码如下:
public class Solution { public int countPrimes(int n) { boolean[] prime = new boolean[n]; Arrays.fill(prime, true); int count = 0; for(int i = 2; i < n; i++) { if(prime[i]) { for(int j = i * 2; j < n; j += i) { prime[j] = false; } } } for(int i = 2; i < prime.length; i++) { if(prime[i]) count ++; } return count; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 221Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 219You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 336Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 325Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 443Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 498Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 423Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 611Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 420The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 378Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 511Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 521Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 364All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 853Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 876Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 547Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 592Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 757Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 724You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 623For a undirected graph with tre ...
相关推荐
LeetCode 204的题目是“计数质数”(Count Primes),要求统计所有小于非负整数n的质数的数量。解决这个问题的关键是高效地识别质数,并减少不必要的重复检查。在C#中,一个常见的解决方案是使用埃拉托斯特尼筛法...
countPrimes(int n) { int count = 0; for(int i = 2; i < n; i++) { bool sign = true; for(int j = 2; j < i; j++) { if(i % j == 0) { sign = false; break; } } if(sign) count++; ; } return count; } ...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
本文实例讲述了JS实现计算小于非负数n的... var countPrimes = function(n) { let flagArray = [], result = 0; for(let i = 2; i < n; i++){ if(flagArray[i] === undefined){ flagArray[i] = 1; result++;
本文为大家分享了多种方法求质数...def countPrimes1(self, n): :type n: int :rtype: int if n<=2: return 0 else: res=[] for i in range(2,n): flag=0 # 质数标志,=0表示质数 for j in range(2,i):
countPrimes(int n) { if (n isPrime(n+1, 1); int count = 1; for (int i=3; i<n; i+=2){ if (isPrime[i]==1) { count++; for (int j = i; j<=n/i; j+=2) isPrime[j*i] = 0; } } return count; } :star: 2020...
leetcode数组下标大于间距 problems 1.Exercise ...CountPrimes:计算小于n的素数个数,数学 bestsubstring:将字符串尽可能分割为多个部分,各子串不包含相同字符暴力 2pow:计算2的N次幂 dp/ chrous:相
go-prime-发电机 Golang 上的质数生成器具有超时功能 安装 ... #如何 package main ... //primes count fmt . Println ( len ( primes )) } func generate ( maxN int , timeout time. Duration ) [] i
名称> http://<server>:<port>/hello/<name> 素数100 http://<server>:<port>/primes/ 素数<count> http://<server>:<port>/primes/<count>构建并运行简单说明(Debian / Ubuntu) 安装要求: apt updateapt install...
名称> http://<server>:<port>/hello/<name> 素数100 http://<server>:<port>/primes/ 素数<count> http://<server>:<port>/primes/<count>构建并运行简单说明(Debian / Ubuntu) 安装要求: apt updateapt install...
名称> http://<server>:<port>/hello/<name> 素数100 http://<server>:<port>/primes/ 素数<count> http://<server>:<port>/primes/<count>构建并运行简单说明(Debian / Ubuntu) 安装要求: apt updateapt install...