Problem
Given an integer n, return the number of prime numbers that are strictly less than n.
Algorithm
Calculate the number of prime numbers using Eratosthenes’ method.
Code
class Solution:
def countPrimes(self, n: int) -> int:
cnts = 0
prime = [1] * (n + 1)
for i in range(2, n):
if not prime[i]:
continue
cnts += 1
for j in range(2, (n + i - 1) // i):
prime[i * j] = 0
return cnts