当前位置:网站首页>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;
}
}

边栏推荐
- 【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
- Using tensorboard to visualize the model, data and training process
- Button wizard play strange learning - go back to the city to buy medicine and add blood
- 音程的知识的总结
- Machine learning terminology
- Tp6 fast installation uses mongodb to add, delete, modify and check
- Type expansion of non ts/js file modules
- Matlab Doppler effect produces vibration signal and processing
- JS inheritance and prototype chain
- MySQL foundation 06 DDL
猜你喜欢

给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】

Daily topic: movement of haystack

Top ten regular spot trading platforms 2022

Assets, vulnerabilities, threats and events of the four elements of safe operation

【FPGA教程案例6】基于vivado核的双口RAM设计与实现

12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet

MySQL basic usage 02

C#应用程序界面开发基础——窗体控制(2)——MDI窗体

Asynchronous, email and scheduled tasks

Cut point of undirected graph
随机推荐
Dotconnect for PostgreSQL data provider
C#应用程序界面开发基础——窗体控制(3)——文件类控件
MySQL foundation 04 MySQL architecture
【第29天】给定一个整数,请你求出它的因子数
2022 cable crane driver examination registration and cable crane driver certificate examination
Database SQL language 01 where condition
tp6快速安装使用MongoDB实现增删改查
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
leetcode:701. Insertion in binary search tree [BST insertion]
Androd gradle's substitution of its use module dependency
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
[自我管理]时间、精力与习惯管理
Compare version number
攻克哈希的基本概念与实现
What are the trading forms of spot gold and what are the profitable advantages?
R language ggplot2 visualization: use ggplot2 to display dataframe data that are all classified variables in the form of thermal diagram, and customize the legend color legend of factor
On Fibonacci sequence
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research
【无标题】
MySQL基础用法02