728x90
class Solution:
def countPrimes(self, n: int) -> int:
return len(self.getPrimaryNum_Eratos(n))
def getPrimaryNum_Eratos(self,N):
nums = [True] * (N + 1)
for i in range(2, len(nums) // 2 + 1):
if nums[i] == True:
for j in range(i+i, N, i):
nums[j] = False
return [i for i in range(2, N) if nums[i] == True]
출처: somjang.tistory.com/entry/leetCode-204-Count-Primes-Python
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Reverse Linked List 파이썬 (0) | 2021.04.05 |
---|---|
[LeetCode] Isomorphic Strings 파이썬 (0) | 2021.04.05 |
[LeetCode] Remove Linked List Elements 파이썬 (linked list) (0) | 2021.04.04 |
[LeetCode] Happy Number 파이썬 (dict, cycle) (0) | 2021.04.04 |
[LeetCode] Fatorial Trailing Zeros 파이썬 (dp) (0) | 2021.04.03 |
댓글