- 浏览: 185209 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 272You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 390Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 504Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 672Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 474The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 435Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 592Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 430All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 907Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 935Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 607Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 697Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 863Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 791You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 731For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Count Primes.java
- **类定义**:`CountPrimes` 类包含一个 `countPrimes` 方法,用于计算小于n的所有质数的数量。 - **方法参数**:`countPrimes` 方法接受一个整数参数n。 - **初始化质数数组**:`boolean[] isPrime = new boolean...
2. **区间内素数计数**:接下来,你需要编写一个方法`countPrimes(int start, int end)`,用于统计start和end(包括)之间的素数数量。这可以通过遍历该区间并对每个数应用`isPrime()`函数来实现。 ```java public ...
1.7 Count Primes(质数的个数) 2. Algorithm Implementation Questions (算法实现题) 3. Linked List Questions(链表相关问题) 4. Array Questions(数组相关问题) 5. Binary Tree Questions(二叉树相关问题...
java入门 java_leetcode题解之204_Count_Primes
LeetCode 204的题目是“计数质数”(Count Primes),要求统计所有小于非负整数n的质数的数量。解决这个问题的关键是高效地识别质数,并减少不必要的重复检查。在C#中,一个常见的解决方案是使用埃拉托斯特尼筛法...
c++ C++_leetcode题解之204_Count_Primes.cpp
python python_leetcode题解之204_Count_Primes.py
def countPrimes(n): if n return 0 primes = [True] * n primes[0], primes[1] = [False, False] for i in range(2, int(n**0.5)+1): if primes[i]: for j in range(i*i, n, i): primes[j] = False ...
int countPrimes(int n) { vector<bool> isPrime(n+1, true); int count = 0; isPrime[0] = isPrime[1] = false; for (int p = 2; p * p ; p++) { if (isPrime[p]) { for (int i = p * p; i ; i += p) { ...
public int countPrimes(int n) { if (n ) return 0; boolean[] isPrime = new boolean[n]; Arrays.fill(isPrime, true); int count = 0; for (int i = 2; i * i ; i++) { if (isPrime[i]) { count++; for...
29. **Count Primes**:计数素数。可以使用埃拉托斯特尼筛法(Sieve of Eratosthenes)来高效找出一定范围内的素数。 以上就是LeetCode中部分Python编程题目的解析概述,通过这些题目,可以深入理解Python编程、...
int countPrimes() { int count = 0; for(int i = 100; i ; i++) { if(isPrime(i)) { count++; } } return count; } ``` #### 5. 计算整数n的所有因子之和(不包括1与自身) 此功能涉及到寻找一个给定整数...
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....|-----|---------------- | --------------- |...
通过运行`countPrimes(10)`和`countPrimes(100)`,我们可以得到小于10和100的素数数量。通过运行`PrimesList(100,200)`则可以获得100到200之间所有素数的列表。 本文还提到了一些JavaScript编程的高级技巧,包括...
本文为大家分享了多种方法求质数...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...