当前位置:网站首页>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
边栏推荐
- Report on the double computer experiment of scoring system based on 485 bus
- ucore lab7 同步互斥 实验报告
- Differences between select, poll and epoll in i/o multiplexing
- Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
- MySQL数据库(一)
- Mysql database (III) advanced data query statement
- The most detailed postman interface test tutorial in the whole network. An article meets your needs
- 软件测试需求分析之什么是“试纸测试”
- 線程及線程池
- What are the software testing methods? Show you something different
猜你喜欢
線程及線程池
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
ucore lab7
自动化测试你必须要弄懂的问题,精品总结
Report on the double computer experiment of scoring system based on 485 bus
Crawler series of learning while tapping (3): URL de duplication strategy and Implementation
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
遇到程序员不修改bug时怎么办?我教你
Do you know the advantages and disadvantages of several open source automated testing frameworks?
How to become a good software tester? A secret that most people don't know
随机推荐
Knowledge that you need to know when changing to software testing
基于485总线的评分系统双机实验报告
软件测试行业的未来趋势及规划
Mysql database (III) advanced data query statement
ucore lab1 系统软件启动过程 实验报告
CSAPP homework answers chapter 789
How to become a good software tester? A secret that most people don't know
Investment operation steps
Programmers, how to avoid invalid meetings?
ucore lab7
UCORE lab1 system software startup process experimental report
Do you know the performance testing terms to be asked in the software testing interview?
软件测试方法有哪些?带你看点不一样的东西
软件测试需求分析之什么是“试纸测试”
ucore lab 2
How to write the bug report of software test?
Interview answering skills for software testing
UCORE lab7 synchronous mutual exclusion experiment report
Heap, stack, queue
ucore lab8 文件系统 实验报告