当前位置:网站首页>Force buckle 204 Count prime
Force buckle 204 Count prime
2022-07-03 01:27:00 【Ruthless young Fisherman】
subject
Given integer n , return All integers less than nonnegative n The number of prime numbers .
Example
Input :n = 10
Output :4
explain : Less than 10 There are a total of 4 individual , They are 2, 3, 5, 7 .
Input :n = 0
Output :0
Input :n = 1
Output :0
source : Power button (LeetCode)
link :https://leetcode.cn/problems/count-primes
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: Ethmoid
Java Realization
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;
}
}
边栏推荐
- Key wizard play strange learning - multithreaded background coordinate recognition
- Is there a handling charge for spot gold investment
- MySQL - database query - basic query
- Work experience of a hard pressed programmer
- Asynchronous, email and scheduled tasks
- R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
- Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
- Strongly connected components of digraph
- 机器学习术语
- Using tensorboard to visualize the model, data and training process
猜你喜欢
[androd] module dependency replacement of gradle's usage skills
C#应用程序界面开发基础——窗体控制(3)——文件类控件
Excel if formula determines whether the two columns are the same
Basic remote connection tool xshell
How is the mask effect achieved in the LPL ban/pick selection stage?
Arduino DY-SV17F自动语音播报
leetcode 2097 — 合法重新排列数对
Androd gradle's substitution of its use module dependency
一位苦逼程序员的找工作经历
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
随机推荐
Leetcode 6103 - minimum fraction to delete an edge from the tree
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
Basic concept and implementation of overcoming hash
一比特苦逼程序員的找工作經曆
What are the trading forms of spot gold and what are the profitable advantages?
Cut point of undirected graph
Create your first Kivy program Hello word (tutorial includes source code)
Androd gradle's substitution of its use module dependency
Matlab finds the position of a row or column in the matrix
【FPGA教程案例5】基于vivado核的ROM设计与实现
2022 cable crane driver examination registration and cable crane driver certificate examination
MySQL foundation 04 MySQL architecture
【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
dotConnect for PostgreSQL数据提供程序
按键精灵打怪学习-前台和内网发送后台验证码
[FPGA tutorial case 5] ROM design and Implementation Based on vivado core
Excel removes the data after the decimal point and rounds the number
Button wizard play strange learning - automatic return to the city route judgment
tp6快速安装使用MongoDB实现增删改查
How is the mask effect achieved in the LPL ban/pick selection stage?