当前位置:网站首页>力扣 204. 计数质数
力扣 204. 计数质数
2022-07-03 01:02:00 【冷酷的摸鱼小将】
题目
给定整数 n ,返回 所有小于非负整数 n 的质数的数量 。
示例
输入:n = 10
输出:4
解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
输入:n = 0
输出:0
输入:n = 1
输出:0
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-primes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法1:埃氏筛

Java实现
class Solution {
public int countPrimes(int n) {
int[] isPrime = new int[n];
Arrays.fill(isPrime, 1);
int ans = 0;
for (int i = 2; i < n; i++) {
if (isPrime[i] == 1) {
ans++;
if ((long) i * i < n) {
for (int j = i * i; j < n; j += i) {
isPrime[j] = 0;
}
}
}
}
return ans;
}
}

边栏推荐
- 【FPGA教程案例6】基于vivado核的双口RAM设计与实现
- [fh-gfsk] fh-gfsk signal analysis and blind demodulation research
- JS inheritance and prototype chain
- 信息熵的基础
- Assets, vulnerabilities, threats and events of the four elements of safe operation
- MySQL foundation 05 DML language
- Niu Ke swipes questions and clocks in
- Daily topic: movement of haystack
- Draw love with go+ to express love to her beloved
- dotConnect for PostgreSQL数据提供程序
猜你喜欢

Basic remote connection tool xshell

Leetcode 6103 - minimum fraction to delete an edge from the tree
![[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)](/img/ca/1d2473ae51c59b84864352eb17de94.jpg)
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)

Cut point of undirected graph

Basis of information entropy

Machine learning terminology

MySQL foundation 05 DML language

Niu Ke swipes questions and clocks in

How wide does the dual inline for bread board need?

Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
随机推荐
Basis of information entropy
强化学习 Q-learning 实例详解
Strongly connected components of digraph
[Arduino experiment 17 L298N motor drive module]
[FPGA tutorial case 5] ROM design and Implementation Based on vivado core
Thinkphp+redis realizes simple lottery
攻克哈希的基本概念与实现
Every k nodes in the linked list are flipped
【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
Delete duplicate elements in the ordered linked list -ii
tp6快速安装使用MongoDB实现增删改查
Trois tâches principales: asynchrone, courrier et timing
On Fibonacci sequence
[system analyst's road] Chapter V double disk software engineering (development model development method)
Button wizard play strange learning - go back to the city to buy medicine and add blood
MySQL --- 数据库查询 - 条件查询
The meaning of wildcard, patsubst and notdir in makefile
Top ten regular spot trading platforms 2022
matlab 多普勒效应产生振动信号和处理
Machine learning terminology