当前位置:网站首页>LeetCode#204. Count prime
LeetCode#204. Count prime
2022-07-06 15:21:00 【Rufeng ZHHH】
Count all less than nonnegative integers n The number of prime numbers .
Example 1:
Input :n = 10
Output :4
explain : Less than 10 There are a total of 4 individual , They are 2, 3, 5, 7 .
Example 2:
Input :n = 0
Output :0
Example 3:
Input :n = 1
Output :0
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/count-primes
We use Eratosthene screening _ Baidu Encyclopedia
class Solution:
def countPrimes(self, n: int) -> int:
if n <3:
return 0
else:
dp=[1]*n # Create array ( First, it defaults to all primes )
dp[0],dp[1]=0,0 #0 and 1 Not prime
for i in range(2,n):
for i in range(i*i,n,i): # To optimize , from i*i Just start , Reduce duplication
dp[i]=0
return sum(dp) # Prime numbers all correspond to 1
边栏推荐
- Dlib detects blink times based on video stream
- Winter vacation daily question - maximum number of balloons
- C4D quick start tutorial - creating models
- Stc-b learning board buzzer plays music 2.0
- How to write the bug report of software test?
- Mysql database (III) advanced data query statement
- MySQL transactions
- Leetcode notes - dynamic planning -day6
- UCORE lab1 system software startup process experimental report
- Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Future trend and planning of software testing industry
CSAPP shell lab experiment report
Winter vacation daily question - maximum number of balloons
遇到程序员不修改bug时怎么办?我教你
CSAPP家庭作業答案7 8 9章
Brief introduction to libevent
Sorting odd and even subscripts respectively for leetcode simple problem
Leetcode notes - dynamic planning -day6
MySQL数据库(四)事务和函数
Want to change jobs? Do you know the seven skills you need to master in the interview software test
随机推荐
JDBC介绍
線程及線程池
How to write the bug report of software test?
Interface test interview questions and reference answers, easy to grasp the interviewer
MySQL数据库(三)高级数据查询语句
Interview answering skills for software testing
What to do when programmers don't modify bugs? I teach you
Scoring system based on 485 bus
Report on the double computer experiment of scoring system based on 485 bus
MySQL transactions
STC-B学习板蜂鸣器播放音乐2.0
Stc-b learning board buzzer plays music
安全测试入门介绍
Nest and merge new videos, and preset new video titles
What if software testing is too busy to study?
Description of Vos storage space, bandwidth occupation and PPS requirements
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
Collection collection and map collection
Collection集合与Map集合
自动化测试中敏捷测试怎么做?