File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 190190- [ 200 Number of Islands] ( https://leetcode.com/problems/number-of-islands/description/ )
191191- [ 201 Bitwise AND of Numbers Range] ( https://leetcode.com/problems/bitwise-and-of-numbers-range/description/ )
192192- [ 202 Happy Number] ( https://leetcode.com/problems/happy-number/description/ )
193+ - [ 204 Count Primes] ( https://leetcode.com/problems/count-primes/description/ )
193194- [ 205 Isomorphic Strings] ( https://leetcode.com/problems/isomorphic-strings/description/ )
194195- [ 207 Course Schedule] ( https://leetcode.com/problems/course-schedule/description/ )
195196- [ 208 Implement Trie (Prefix Tree)] ( https://leetcode.com/problems/implement-trie-prefix-tree/description/ )
Original file line number Diff line number Diff line change 1+ class Solution :
2+ """Base class for all LeetCode Problems."""
3+
4+ def countPrimes (self , n : int ) -> int :
5+ """
6+ Given an integer n, return the number of prime numbers that are strictly less
7+ than n.
8+ """
9+ # Time Complexity: O(n*sqrt(n))
10+ # Space Complexity: O(n)
11+ if n == 0 or n == 1 :
12+ return 0
13+ primes = [True for i in range (n )]
14+ primes [0 ] = primes [1 ] = False
15+ p = 2
16+ while p * p < n :
17+ if primes [p ]:
18+ for i in range (p * p , n , p ):
19+ primes [i ] = False
20+ p += 1
21+ return sum (primes )
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+ from awesome_python_leetcode ._204_count_primes import Solution
4+
5+
6+ @pytest .mark .parametrize (
7+ argnames = ["n" , "expected" ],
8+ argvalues = [
9+ (10 , 4 ),
10+ (0 , 0 ),
11+ (1 , 0 ),
12+ ],
13+ )
14+ def test_func (n : int , expected : int ):
15+ """Tests the solution of a LeetCode problem."""
16+ primes = Solution ().countPrimes (n )
17+ assert primes == expected
You can’t perform that action at this time.
0 commit comments